Java Swing JLabel.setIcon() 没有按我期望的方式工作

标签 java image swing nullpointerexception jlabel

我有一个可能很容易解决的问题。我使用 Intellij Idea 构建了一个 GUI 表单。现在我正在尝试更改 imageLabel JLabel 的 imageIcon。

我不太明白为什么,但是当我使用 JLabel.setIcon() 时,它既不抛出异常也不显示图像。我不知道有什么问题。这看起来是一个非常简单的命令。

(我添加了 ico.getImage().flush(); 行,因为当我在周围搜索时,人们说你必须在显示图像之前刷新图像。我实际上不知道该行的作用。)

感谢您的帮助。

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);
        }
    }

}

最佳答案

那一小段代码存在很多问题。删除许多冗余组件后,对类的引用不明显,修复了 NullPointerException 的两个实例,删除了刷新图像的调用,并通过新创建的 修复了问题>App() 已经存在,它“有效”。但它仍然很糟糕,我建议扔掉这些东西,然后重新开始引用 JavaDocs 来研究随机人们推荐的东西,以及 Java 教程来了解 GUI 开发的基础知识。

所以这是“固定”代码:它将加载图像,但随后需要拉伸(stretch) 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);
        }
    }
}

关于Java Swing JLabel.setIcon() 没有按我期望的方式工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61091516/

相关文章:

java - 为什么我收到错误: cannot find symbol for JComponent?

java - 将颜色更改为 JTable 中未选中的行

java - 即使代码未更改,JTextField 也不会随机出现在程序中

java - 我在我的 IP 地址上找不到我的端口号

java - 按升序和降序排列字符串

java - 如何在 Android 上使用 SHA-256

html - 在 ul 内的图像顶部覆盖列表项元素,而无需设置背景图像

java - 如何获取模型中所有覆盖的操作/属性?

python - 使用 PYTHON 在 "form"中同时显示图像和文本框

c# - 缩放、旋转和裁剪图像