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

时间:2013-03-11 01:51:49

标签: java swing jframe jlabel imageicon

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

` 请帮帮我,或者给我一些样品......谢谢!

1 个答案:

答案 0 :(得分:1)

看看以下几行:

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

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

stacktrace

NPEImageLoading的根本原因的良好指标

您可以使用已panel实例化的ButtonListener listener = new ButtonListener(); 当前实例:

{{1}}