0

我是新来的,试图编写一个在 JFrame 上有一个按钮的代码并单击它可以打开一个 JFileChooser 来选择一个图像,在用户选择后,它会在 thr JFrame 上显示它,但我一直得到一个
Exception in thread "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();
}

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

4

1 回答 1

1

看看这些行:

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

您正在将 a 添加ButtonListenerbutton附加到ImageLoading. panel在方法中被实例化imageLoading,但在这个新实例中没有调用它,导致NullPointerException稍后在您尝试调用时

panel.add(lb);

(这stacktrace是一个很好的指示 的根本原因NPE

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

ButtonListener listener = new ButtonListener();
于 2013-03-11T01:55:25.967 回答