What can I do in order to recognize button 2
by do_something
function? I want to changing button2
text after clicking on it, but I received an error: button2 cannot be resolved
.
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);
}
}