【发布时间】:2012-06-04 19:43:43
【问题描述】:
代码给出了空指针异常的错误.....要做什么?
import java.awt.event.*;
import javax.swing.*;
public class Gui implements ActionListener{
JButton button;
public Gui(){
JFrame frame=new JFrame();
JButton button =new JButton("click me!");
button.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(button);
frame.setSize(270,300);
frame.setVisible(true);
}
public static void main(String[] args){
new Gui();
}
public void actionPerformed(ActionEvent e){
button.setText("I've been clicked");
}
}
【问题讨论】:
-
我认为在尝试 GUI 开发之前,您需要了解更多有关 Java 的知识。
标签: java swing nullpointerexception scope