一个菜问题:无法从静态上下文中引用非静态变量
我的程序是这样的import javax.swing.*;
import java.awt.event.*;
public class A implements ActionListener {
JButton button;
int ClickNum = 0;
public static void main(String[]args){
A gui = new A();
while( ClickNum < 9){
gui.gogo();
}
System.exit(0);
}
public void gogo() {
JFrame frame = new JFrame();
button = new JButton("click me");
button.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(button);
frame.setSize(500,500);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
ClickNum++;
button.setText("I've been clicked for " + ClickNum );
}
}
但编译后的结果是
无法从静态上下文中引用非静态ClickNum
本人是个新人,希望各位大虾详细指点。