I have added a button, that is initially "Please Wait" and changes to "Click Here" as the timer starts. I need to make it so that if the button is clicked, whilst the text is "Please Wait", it starts the timer, rather than having the timer start itself after the 3000ms.
I also am wondering how I could make the restart button, actually restart the test.
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.Timer;
public class Main extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
//Width and Height of the JFrame
public static final int WIDTH = 550;
public static final int HEIGHT = 550;
//Width and Height of the Add Button
public static final int WIDTH1 = 450;
public static final int HEIGHT1 = 450;
//Width and Height of Restart Button
public static final int WIDTH2 = 125;
public static final int HEIGHT2 = 25;
//Adding the 3 main components
//This is the label that counts the clicks
private JLabel lblValue;
//This is the integer that counts the clicks
private int clicks = 0;
//This is the timer
private int count = 6;
public static void main(String[] args)
{
// This creates two instances of Main,
// which creates two different windows.
Main counter1 = new Main( );
counter1.setVisible(true);
Main counter2 = new Main( );
counter2.setVisible(true);
}
public Main( )
{
//Title that appears at the top of the page
setTitle("CPS Test");
//Close when the close button is pressed
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Setting size of the frame
setSize(WIDTH, HEIGHT);
setMaximumSize(new Dimension(WIDTH, HEIGHT));
setMinimumSize(new Dimension(WIDTH, HEIGHT));
setLayout(new FlowLayout( ));
// This is the label for the amount of clicks
lblValue = new JLabel("0");
//Adding the counter onto the screen
add(lblValue);
//This is the button that adds 1 every time clicked
JButton addButton = new JButton("Please Wait");
addButton.addActionListener(this);
//Setting size for button
addButton.setSize(new Dimension(WIDTH1, HEIGHT1));
addButton.setPreferredSize(new Dimension(WIDTH1, HEIGHT1));
addButton.setMinimumSize(new Dimension(WIDTH1, HEIGHT1));
addButton.setMaximumSize(new Dimension(WIDTH1, HEIGHT1));
//Adding button onto screen
add(addButton);
JButton resetButton = new JButton("Restart");
//Setting size for button
resetButton.setSize(new Dimension(WIDTH2, HEIGHT2));
resetButton.setPreferredSize(new Dimension(WIDTH2, HEIGHT2));
resetButton.setMinimumSize(new Dimension(WIDTH2, HEIGHT2));
resetButton.setMaximumSize(new Dimension(WIDTH2, HEIGHT2));
//Adds the label for the timer. This changes after 1000 milliseconds to 1, and then counts every second
JLabel label = new JLabel("Starting Soon");
setLayout(new GridBagLayout());
//Adding timer onto the screen
add(label);
//Delay in milliseconds between number adding
Timer timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Add 1 to count every interval of 1000 milliseconds
count--;
//Timer keeps ticking till this number is achieved
if (count >= 0) {
label.setText(Integer.toString(count));
}
//Will stop timer, remove the click button and will calculate how many clicks you got per second
else {
((Timer) (e.getSource())).stop();
remove(addButton);
label.setText("");
lblValue.setText("You clicked " + clicks / 5d + " clicks per second.");
add(resetButton);
}
//Will change text from "Please Wait" to "Click Here" after timer delay has finished
if (count == 5) {
addButton.setText("Click Here");
}
}
});
//Timer wont start for 3000 milliseconds
timer.setInitialDelay(3000);
//Start Timer
timer.start();
}
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand( );
//This will only work whilst the text is "Click Here". Wont work for 1000 milliseconds as the text is "Please Wait"
if (actionCommand.equals("Click Here"))
{
//Adds 1 to the click counter ever time it is clicked
lblValue.setText(Integer.toString(clicks += 1));
}
}
}```