java - 无法将 imageicon 设置为 JLabel,始终为 null

标签 java swing jframe jlabel imageicon

我是 Swing 新手,尝试编写一个在 JFrame 上有一个按钮的代码,单击它可以打开 JFileChooser 来选择图像,用户选择后,它将在 JFrame 上显示它,但我不断收到 < br/>线程“AWT-EventQueue-0”java.lang.NullPointerException中出现异常
我发现我无法将选定的图像图标设置到JPanel,这让我发疯,救命请我!!!
我的代码是:

public class ImageLoading extends JFrame{

private static final int FRAME_WIDTH = 500;
private static final int FRAME_HEIFHT = 500;
private BufferedImage mImage;
private JFrame frm;
private JPanel panel;
private JButton button;
String name,name1;

public class ButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        String source=filechoose();
        File inputFile = new File(source);
        System.out.println("File Directory: " + inputFile.toString());
        try {
            mImage = ImageIO.read(inputFile);
            System.out.println("Buffered Image: " + mImage.toString());
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }

            ImageIcon image =  new ImageIcon(mImage);
            System.out.println("Image Icon: " + image.toString() + " :::  " +image.getIconHeight());
            JLabel lb = new JLabel(image);// HERE I GET EXCEPTION
            System.out.println("Image Label: " + lb.toString());
            panel.add(lb);
            panel.revalidate();
            panel.repaint();
            frm.pack();

    }
}


public void imageLoading(){
    frm = new JFrame("image loading test");
    frm.setLayout(null);
    JLabel label2 = new JLabel("Open a picture");
    button = new JButton("Open");
    panel = new JPanel();
    panel.setBounds(10, 10, 400, 400);

    panel.add(button);
    panel.add(label2);

    ImageLoading load = new ImageLoading();
    ButtonListener listener = load.new ButtonListener();

    button.addActionListener(listener);

    frm.add(panel);

    frm.setSize(FRAME_WIDTH,FRAME_HEIFHT);
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frm.setVisible(true);
}

public String filechoose(){
    JFileChooser chooser = new JFileChooser();

    chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
        public boolean accept(File f) {
            name = f.getName().toLowerCase();
            return name.endsWith(".gif") || name.endsWith(".jpg")
                    || name.endsWith(".jpeg") || f.isDirectory();
        }

        public String getDescription() {
            return "Image files";
        }
    });

    int r = chooser.showOpenDialog(this);

    if (r == JFileChooser.APPROVE_OPTION) {
        name1 = chooser.getSelectedFile().getAbsolutePath();
        StringBuffer sb=new StringBuffer();
        sb.append(name1);

        int l=sb.length();
        for(int i=0;i<l;i++){
            if(sb.charAt(i)=='\\'){
                sb.insert(i, "\\");                 
            }
        }
    }
    return name1;
}

public static void main(String a[]){

    ImageLoading i = new ImageLoading();
    i.imageLoading();
}

` 请帮我解决这个问题,或者给我一些这样做的示例......谢谢!

最佳答案

看看这些行:

ImageLoading load = new ImageLoading(); // not needed
ButtonListener listener = load.new ButtonListener();
button.addActionListener(listener);

您正在将 ButtonListener 添加到附加到新的(不必要的)ImageLoading 实例的 button 中。 panelimageLoading 方法中实例化,但在这个新实例中不会调用它,导致稍后当您尝试调用时出现 NullPointerException

panel.add(lb);

(stacktrace 可以很好地指示 NPE 的根本原因)

您可以使用已实例化 panelImageLoading当前实例:

ButtonListener listener = new ButtonListener();

关于java - 无法将 imageicon 设置为 JLabel,始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15330036/

相关文章:

java - 如何在 JComponent.revalidate 和 Container.validate 之间进行选择

java - PaintComponent 不适用于绘制形状

java - 如何将 JPanel 添加到自动生成的 netbeans JFrame 中?

java - Spring Boot 返回 403 虽然我有管理员权限

java - lambda 不需要异常处理

java - Springboot RabbitMQ 未接收匿名队列

java - 使用 Matlab 创建 REST-ful 服务?

java - Spring Bean 在应用程序重新启动后仍然存在吗?

Java Swing - JLabel 中出现空格

java - JTextField 无法调整大小