I am creating a simple stopwatch program with 3 buttons ( Start, Stop and Reset) and a simple JLabel to display the time. I am adding an action listener to the start button so when the button is clicked, the timer starts the clock on the label.
However, when I try to add the timer variable inside the button event, it doesn't allow me to do so. Can anyone suggest what I did wrong or how to improve it?
Code:
public class GuiStopwatch {
public static void main(String[] args) {
JFrame frame = new JFrame("Stopwatch");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setLayout(null);
JButton Startbtn = new JButton("START");
JButton Stopbtn = new JButton("STOP");
JButton Reset = new JButton("RESET");
JLabel time = new JLabel("Time shows here");
panel.add(Startbtn);
panel.add(Stopbtn);
panel.add(Reset);
panel.add(time);
Startbtn.setBounds(50, 150, 100, 35);
Stopbtn.setBounds(50, 200, 100, 35);
Reset.setBounds(50, 250, 100, 35);
time.setBounds(50, 350, 100, 35);
time.setBackground(Color.black);
time.setForeground(Color.red);
frame.add(panel);
Startbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Timer timer = new Timer(1,new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
})
}
});
EDIT:
The error is it says there is no constructor defined for Timer(int, ActionListener).