I have a button in a simple application I'm making which will increment a variable by one once pressed. This is it's code:
public class GUI implements ActionListener {
int clicks = 0;
int autoClickLevel = 0;
JLabel label;
JFrame frame;
JPanel panel;
public GUI() {
JButton button = new JButton("Click me!");
button.addActionListener(this);
panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(60, 100, 30, 100));
panel.setLayout(new GridLayout(0, 1));
panel.add(button);
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
public static void main(String[] args) {
new GUI();
}
@Override
public void actionPerformed(ActionEvent e) {
clicks++;
}
I would like to know how to make a separate button (which I have already made, and it shows up; JButton button2 = new JButton("Click me too!");
) which changes a separate variable. button2.addActionListener(this);
[plus different ways of doing it,] instead increments the clicks
variable instead of a separate clicks2
variable.
My code is a bit of a mess regarding this, and this second button's script doesn't work at all. I'm also fairly new to Java so I'm not much good at figuring out this either. What's a good way to make the second button increment the other variable?