3

我正在尝试编写一个代码,将使用 JFileChooser 选择的图像显示到另一个 JFrame 中。我尝试了下面的代码,但只得到了以下错误。

Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:228)
at power.<init>(fCGUI.java:53)
at fCGUI.main(fCGUI.java:11)

这是代码:

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

public class fCGUI
{
    public static void main(String []args)
    {
        power p=new power();
        p.setVisible(true);
    }
}

class power extends JFrame
{
    JFileChooser chooser;
    BufferedImage img;
    JButton button,button2;
    JFrame comp;
    String filename;
    File file ; 

    public power()
    {
        setSize(450,450);
        panel.setLayout(new BorderLayout());

        JPanel panel=new JPanel();
        getContentPane().add(panel);
        button =new JButton("press");

        panel.add(button,BorderLayout.NORTH);

        chooser = new JFileChooser();

        ActionListener action=new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (e.getSource()==button)
                {
                    chooser.showOpenDialog(null);
                    file = chooser.getSelectedFile();

                    try
                    {
                        img=ImageIO.read(file);
                    }
                    catch(IOException e1) {}
                }

                if (e.getSource()==button2)
                {
                    comp.setVisible(true);
                }
            }
        };

        ImageIcon icon=new ImageIcon(img);
        JLabel label=new JLabel(icon);

        JPanel secpanel=new JPanel();

        comp=new JFrame();
        comp.setSize(650,500);
        comp.setLayout(new BorderLayout());
        comp.setTitle("View Report");

        JRootPane compPane=comp.getRootPane();
        Container contePane=compPane.getContentPane();
        contePane.add(secpanel);

        secpanel.add(label,BorderLayout.CENTER);

        button2=new JButton("access");
        button2.addActionListener(action);
        button.addActionListener(action);

        panel.add(button2,BorderLayout.SOUTH);
    }
}
4

2 回答 2

3

只有在用户单击按钮并选择要显示的文件后,该值img才会具有实际值。直到此时, 的值imgnull,所以当它继续通过您的方法并调用 lineImageIcon icon=new ImageIcon(img);时,它​​正在尝试为 创建一个ImageIcon对象null

要更正此问题,您应该只ImageIcon在用户选择文件时创建。这是一个应该更接近正常工作的更改。(请参阅评论//ADDED//REMOVED下面的代码以查看更改...

...
class power extends JFrame {
    JFileChooser chooser;
    BufferedImage img;
    JButton button,button2;
    JFrame comp;
    String filename;
    File file ; 
    JLabel label; // ADDED

    public power() {
    ...
            public void actionPerformed(ActionEvent e) {
                if (e.getSource()==button) {
                    chooser.showOpenDialog(null);
                    file = chooser.getSelectedFile();

                    try {
                        img=ImageIO.read(file);
                        ImageIcon icon=new ImageIcon(img); // ADDED
                        label.setIcon(icon); // ADDED

                        Dimension imageSize = new Dimension(icon.getIconWidth(),icon.getIconHeight()); // ADDED
                        label.setPreferredSize(imageSize); // ADDED

                        label.revalidate(); // ADDED
                        label.repaint(); // ADDED
                    }
                    catch(IOException e1) {}
                }

                if (e.getSource()==button2){
                    comp.setVisible(true);
                }
            }
        };

        //ImageIcon icon=new ImageIcon(img); // REMOVED
        //JLabel label=new JLabel(icon); // REMOVED
        label = new JLabel(); // ADDED

        JPanel secpanel=new JPanel();
        ...

为了解释我改变了什么......

  1. 当您第一次启动程序时,label现在将创建为空。JLabel它也被存储为全局变量,因此我们以后可以访问它
  2. 单击按钮时img,会像以前一样创建 ,然后将其加载到您的labelusingsetIcon();
  3. 调整标签的大小,然后revalidate()确保repaint()图像在设置后被绘制。
于 2012-05-24T12:38:05.830 回答
1

仅在按下该ImageIO.read特定时才会执行。JButton那是因为它在ActionListener. 因此,imgis null 会导致NullPointerException何时ImageIcon icon=new ImageIcon(img)执行。

于 2012-05-24T12:03:54.057 回答