level 1
爱新觉罗0衮衮
楼主
一段程序:
public class TestBox implements ActionListener {
//JButton button;
//JFrame frame;
public static void main(String[] args) {
TestBox box = new TestBox();
box.go();
}
public void go(){
JFrame frame = new JFrame();
JButton button = new JButton("click me");
button.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.setSize(300,300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event){
button.setText("I've been clicked!");
}
}
运行后,点击button后异常:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
button cannot be resolved
但如果在类中声明了frame或button(去掉实例变量注释任意一行),程序运行没问题,请问各位大神这是为什么?
2014年06月29日 09点06分
1
public class TestBox implements ActionListener {
//JButton button;
//JFrame frame;
public static void main(String[] args) {
TestBox box = new TestBox();
box.go();
}
public void go(){
JFrame frame = new JFrame();
JButton button = new JButton("click me");
button.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.setSize(300,300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event){
button.setText("I've been clicked!");
}
}
运行后,点击button后异常:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
button cannot be resolved
但如果在类中声明了frame或button(去掉实例变量注释任意一行),程序运行没问题,请问各位大神这是为什么?