我是java的新手,目前通過HeadFirst java book教我自己。 我所經歷的GUI界面,並從書中的代碼似乎並沒有運行,eclipse中的GUI界面異常
import javax.swing.*;
import java.awt.event.*;
public class SimpleGui1 implements ActionListener {
JButton Button;
public static void main(String[] args) {
SimpleGui1 gui = new SimpleGui1();
gui.go();
}
public void go(){
JFrame frame = new JFrame();
JButton button = new JButton("click me");
button.addActionListener(this);
frame.getContentPane().add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
public void actionPerformed (ActionEvent event) {
Button.setText("I have been clicked");
}
}
The exception :
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
誰能告訴我有什麼不對?
什麼是例外? – Kal 2013-04-25 20:46:33
我猜NullPointerException這裏:** Button.setText(「我已被點擊」); ** – Markus 2013-04-25 20:47:42