带图像

时间:2016-09-05 18:44:30

标签: java swing nullpointerexception

因此,在制作以下程序时,我会收到如下所述的错误: 我不知道为什么IDE无法找到我的图像 并且图像与.java文件位于同一文件夹中。 我正在使用eclipse neon

Exception in thread "main" java.lang.NullPointerException 
at javax.swing.ImageIcon.<init>(Unknown Source)
at gui_22.gui.<init>(gui.java:24)
at gui_22.MAIN.main(MAIN.java:6)

代码如下:

主类

import javax.swing.JFrame;
public class MAIN(){
    public static void main(String[] args);
      gui go = new gui();
      go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      go.setSize(300,200);
      go.setVisible(true);
    }
}

Gui Class

import java.awt.FlowLayout;
import.java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;




 public class gui extends JFrame{
 private JButton reg;// to create buttons 
private JButton custom;// Same as above

public gui(){
              super("The Title"); //Allows access to the superclass.
              setLayout(new FlowLayout());

              reg = new JButton("reg Button");
              add(reg);

              Icon b = new ImageIcon(getClass().getResource("b.png"));
              Icon x = new ImageIcon(getClass().getResource("a.png"));

              custom = new JButton("Custom",b);
              custom.setRolloverIcon(x);
              add(custom);

             HandlerClass handler = new HandlerClass();
             reg.addActionListener(handler);//adding a handler for the button
             custom.addActionListener(handler);



}

         private class HandlerClass implements ActionListener{
         public void actionPerformed(ActionEvent event){
            JOptionPane.showMessageDialog(null,String.format("%s", event.getActionCommand()));
         }  
  }

}

0 个答案:

没有答案