import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Form1 {
private JPanel panel1;
private JButton button1;
public Form1() {
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked");
}
});
}
public static void main(String args[]){
JFrame frame = new JFrame("Form 1");
frame.setContentPane(new Form1().panel1);
frame.pack();
frame.setVisible(true);
}
}
该错误与setContentPanein main 方法有关,然后与actionListener. 我在下面发布了确切的错误。为什么是这样?我使用 IntelliJ IDEA GUI 表单创建器创建了它。
Exception in thread "main" java.lang.NullPointerException
at Form1.<init>(Form1.java:12)
at Form1.main(Form1.java:22)
慕娘9325324
温温酱
相关分类