我是新手,尝试编写一个在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();
}


`
请帮我解决这个问题,或给我一些示例。

最佳答案

看一下这些行:

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


您正在将ButtonListener添加到附加到button新(且不必要)实例的ImageLoading中。 panel是在imageLoading方法中实例化的,但是在此新实例中未调用此方法,导致稍后在尝试调用NullPointerException

panel.add(lb);


stacktraceNPE根本原因的良好指示)

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

ButtonListener listener = new ButtonListener();

10-06 01:17