I'm trying to make a CPS (clicks per second) GUI, which, as the name implies, calculates your clicks/second by dividing the number of clicks within a span of 5 seconds by 5. When running the program, however, I don't want the timer to start as soon as the window opens. Rather, I want the timer to start once the first click is made. I only want to be able to click while the timer is still going. When the timer reaches 0, then the button should disappear. If someone could tell me how to use the timer, that would be great. (Sorry if it seems like I am trying to make you do it for me. I am still trying to learn GUI basics).
Here's code from a YouTube tutorial for framework: Also, check out this link (cpstest.org) if you need an idea of the mechanics involved.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CPS_Test implements ActionListener {
private int count = 0;
private JFrame frame;
private JPanel panel;
private JButton button;
private JLabel label;
private Timer timer;
public CPS_Test() {
frame = new JFrame();
button = new JButton("How fast can you click?");
button.addActionListener(this);
label = new JLabel("Number of clicks: 0");
panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
panel.setLayout(new GridLayout(0, 1));
panel.add(button);
panel.add(label);
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("CPS Test");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new GUI_Basic();
}
@Override
public void actionPerformed(ActionEvent e) {
count++;
label.setText("Number of clicks: " + count);
}
}