0

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);
    }
}
RW77
  • 64
  • 1
  • 8
  • Is your question how to implement a timer at all? Cause in your example there is none. Maybe have a look at https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html – TobiSH Feb 19 '21 at 06:27
  • "If someone could tell me how to use the timer, that would be great." Yes. The example was not supposed to have a timer. It was only so one can make necessary adjustments to their answer according to what they saw I had in my code already. Sorry for the confusion. – RW77 Feb 20 '21 at 00:34

1 Answers1

1

If someone could tell me how to use the timer, that would be great.

See: Update a Label with a Swing Timer for a basic example.

I want the timer to start once the first click is made.

  1. In the constructor of your class you create the Timer
  2. In the ActionListener of your button you use the isRunning() method of the Timer. If it is not running, then you start the Timer
  3. When the Timer fires you a) stop the timer and b) make but button invisible
camickr
  • 321,443
  • 19
  • 166
  • 288