-1

画像を正しいワークスペース フォルダー (src) に移動しましたが、このエラー メッセージが表示され続けます.....

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at GUI.<init>(GUI.java:20)
    at main.main(main.java:4)

これがコードです

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

public class GUI extends JFrame {
    private JButton reg;
    private JButton custom;
    public GUI(){
        super("Welcome");
        setLayout(new FlowLayout());
        reg = new JButton("reg button");
        add(reg);

        Icon b = new ImageIcon(getClass().getResource("b.png"));
        Icon a = new ImageIcon(getClass().getResource("a.png"));
        custom = new JButton("Custom", b);
        custom.setRolloverIcon(a);
        add(custom);

        thehandler handle = new thehandler();
        reg.addActionListener(handle);
        custom.addActionListener(handle);

    }

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

}


-----------------------

import javax.swing.JFrame;
public class main {
public static void main(String agrs[]){
    GUI page = new GUI();
    page.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    page.setSize(300,200);
    page.setVisible(true);
}
}
4

2 に答える 2

1

画像は Src->DefaultPackage->**File.java,FileMain.java,b.png,x.png; の下に表示されるはずです。イメージ ファイルが **Src->DefaultPackage-> の下に表示されない場合は、F5ボタンを押すか、パッケージ エクスプローラーを右クリックして [更新] をクリックすると、イメージがSrc->DefaultPackage-> に表示されます。

于 2013-08-25T12:14:59.530 に答える