Java Swing JLabel的setIcon()方法不按我预期的方式工作。

huangapple go评论75阅读模式
英文:

Java Swing JLabel.setIcon() not working the way I expect

问题

public class App
{
    private JPanel mainPanel;
    private JPanel imagePanel;
    private JPanel optionsPanel;
    private JPanel palletesPanel;
    private JPanel buttonsPanel;
    private JPanel originalPalletePanel;
    private JPanel newPalletePanel;
    private JLabel originalPalleteLabel;
    private JLabel newPalleteLabel;
    private JPanel leftButtonsPanel;
    private JPanel rightButtonsPanel;
    private JButton previewButton;
    private JButton revertButton;
    private JButton convertImageButton;
    private JButton matchPalleteButton;
    private JLabel originalPalleteImageLabel;
    private JLabel newPalleteImageLabel;
    private JLabel imageLabel;

    public static void main(String[] args)
    {
        App app = new App();
        JFrame frame = new JFrame("Pixel Pigeon");
        frame.setContentPane(app.mainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        pigeon pigey = new pigeon();
        try
        {
            app.loadImage(frame, app);
        }
        catch(java.io.IOException e)
        {
            e.printStackTrace();
        }

    }

    private void loadImage(JFrame frame, App app) throws IOException
    {
        JFileChooser chooser = new JFileChooser();
        if(chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION)
        {
            BufferedImage img = ImageIO.read(chooser.getSelectedFile());
            ImageIcon ico = new ImageIcon(img);
            ico.getImage().flush();
            app.imageLabel.setIcon(ico);
        }
    }
}
英文:

I have a probably easy to solve problem. I used Intellij Idea to build a GUI form. Now I am trying to change the imageIcon of the imageLabel JLabel.

I don't really understand why but when I use the JLabel.setIcon() it neither throws an exception nor displays the image. I have no idea what is wrong with it. It seems like a very simple command.

( I added ico.getImage().flush(); line because when I was searching around people said you have to flush the image before displaying it. I don't actually know what that line does.)

Thanks for any help.

public class App
{
private JPanel mainPanel;
private JPanel imagePanel;
private JPanel optionsPanel;
private JPanel palletesPanel;
private JPanel buttonsPanel;
private JPanel originalPalletePanel;
private JPanel newPalletePanel;
private JLabel originalPalleteLabel;
private JLabel newPalleteLabel;
private JPanel leftButtonsPanel;
private JPanel rightButtonsPanel;
private JButton previewButton;
private JButton revertButton;
private JButton convertImageButton;
private JButton matchPalleteButton;
private JLabel originalPalleteImageLabel;
private JLabel newPalleteImageLabel;
private JLabel imageLabel;
public static void main(String[] args)
{
App app = new App();
JFrame frame = new JFrame("Pixel Pigeon");
frame.setContentPane(new App().mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
pigeon pigey = new pigeon();
try
{
app.loadImage(frame, app);
}
catch(java.io.IOException e)
{
e.printStackTrace();
}
}
private void loadImage(JFrame frame, App app) throws IOException
{
JFileChooser chooser = new JFileChooser();
if(chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION)
{
BufferedImage img = ImageIO.read(chooser.getSelectedFile());
ImageIcon ico = new ImageIcon(img);
ico.getImage().flush();
app.imageLabel.setIcon(ico);
}
}
}

答案1

得分: 2

这段代码存在许多问题。在移除许多冗余组件、修复两处 NullPointerException 实例、删除刷新图像的调用以及修复已存在的 App() 的新创建问题后,它“可以工作”。但它仍然很糟糕,我建议将其全部删除,重新从 JavaDocs 中了解随机人员推荐的内容,并从 Java 教程中学习 GUI 开发的基础知识。

下面是“修复后”的代码:它将加载一个图像,但 GUI 需要拉伸以使图像可见。

import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.*;
import javax.imageio.ImageIO;

public class App {
    private JPanel mainPanel = new JPanel();
    private JLabel imageLabel = new JLabel();

    public static void main(String[] args) {
        App app = new App();
        JFrame frame = new JFrame("Pixel Pigeon");
        app.mainPanel.add(app.imageLabel);
        frame.setContentPane(app.mainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        try {
            app.loadImage(frame, app);
        }
        catch(java.io.IOException e) {
            e.printStackTrace();
        }
    }

    private void loadImage(JFrame frame, App app) throws IOException {
        JFileChooser chooser = new JFileChooser();
        if(chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
            BufferedImage img = ImageIO.read(chooser.getSelectedFile());
            ImageIcon ico = new ImageIcon(img);
            app.imageLabel.setIcon(ico);
        }
    }
}
英文:

There were a lot of problems with that short section of code. After removing the many redundant components, the reference to a class not in evidence, fixing the two instances of NullPointerException, removing the call to flush the image, and fixing the problem with the new creation of an App() that already existed, it 'works'. But it is still so bad I'd recommend tossing the lot out and starting again with reference to the JavaDocs for investigating things random people recommend, and the Java Tutorial for the basics of GUI development.

So here is the 'fixed' code: It will load an image, but the GUI then needs to be stretched to make the image visible.

import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.*;
import javax.imageio.ImageIO;
public class App {
private JPanel mainPanel = new JPanel();
private JLabel imageLabel = new JLabel();
public static void main(String[] args) {
App app = new App();
JFrame frame = new JFrame("Pixel Pigeon");
app.mainPanel.add(app.imageLabel);
frame.setContentPane(app.mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
try {
app.loadImage(frame, app);
}
catch(java.io.IOException e) {
e.printStackTrace();
}
}
private void loadImage(JFrame frame, App app) throws IOException {
JFileChooser chooser = new JFileChooser();
if(chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
BufferedImage img = ImageIO.read(chooser.getSelectedFile());
ImageIcon ico = new ImageIcon(img);
app.imageLabel.setIcon(ico);
}
}
}

huangapple
  • 本文由 发表于 2020年4月8日 08:28:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/61091516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定