0
如何才能識別button 2
do_something
函數?點擊後我想更改button2
文字,但我收到一個錯誤:button2 cannot be resolved
。通過函數Java Swing對象識別
class myClass {
public static int counter = 0;
public static void do_something() {
button2.setText(Integer.toString(counter));
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(3, 2));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("button 1");
frame.add(button);
JButton button2 = new JButton("button 2");
button2.addActionListener(e -> do_something());
frame.add(button2);
frame.pack();
frame.setVisible(true);
}
}
這是一個範圍的問題。請看看這個問題,讓我們知道它是否解決您的問題:http://stackoverflow.com/questions/4560850/java-variable-scope – ControlAltDel