Hello, I need assistance with this java program I am writing, Im trying to make two buttons and when you click the button, it has a counter, and everytime you click it it prints a message saying I was clicked n times. This is what I have so far and I am pretty stuck. For example, I dont know how to assign a button a counter.
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class ButtonViewer
{
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 360;
int counter1 = 0;
int counter2 = 0;
public class ClickButtonClass implements ActionListener {
public void actionPerformed(actionEvent cbc) {
counter1++;
}
}
public static void main(String[] args)
{
//int counter1 = 0;
//int counter2 = 0;
JFrame frame = new JFrame();
JButton button = new JButton("Click me!");
frame.add(button);
JFrame frame2 = new JFrame();
JButton button2 = new JButton("Click me too!");
frame2.add(button2);
ActionListener listener = new ClickListener();
button.addActionListener(listener);
button2.addActionListener(listener);
//counter1++;
//counter2++;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame2.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setVisible(true);
}
}
And a sperate class file
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
}
}