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

问题描述 投票:0回答:1

我可能很容易解决问题。我使用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);
        }
    }

}
java image swing nullpointerexception jlabel
1个回答
0
投票

那段短代码有很多问题。在删除了许多冗余组件之后,对该类的引用就不明显了,修复了NullPointerException的两个实例,删除了刷新图像的调用,并解决了新创建的App()的问题, ii“作品”。但是仍然很糟糕,我建议您花很多钱,然后再次参考JavaDocs来调查随机人的建议,以及参考Java Tutorial来了解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);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.