Hi,
I'm taking an introductory course in Java, and am creating a GUI typing test program for a final project. The program is not fully complete as you can see; my next step is
to make a timer to countdown from 60 seconds to 0. I can't seem to find how to do this anywhere and the code I have found is too complicated to understand.
What I want to do is, when a button (startBTN) is pressed, it will start the timer and the time will be displayed in a JTextField. All the timer has to do is dynamically show the time going down from 60 to 0. When it gets to zero, it will stop and then I plan to have a JOptionPane window come up with stats about how the user did in the typing test.
This is the program so far in case you're interested.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class TypingTest extends JFrame {
JButton startBTN;
JTextArea areaTA;
JMenu fileM;
JMenuItem exitMI, restartMI;
JTextArea toCopyTA;
JLabel timeLBL;
JTextField timeTF;
JLabel errorsLBL;
JTextField errorsTF;
JLabel speedLBL;
JTextField speedTF;
JLabel blankLBL;
String typed;
public TypingTest()
{
startBTN=new JButton("Start Test");
startBTN.addActionListener(new startButtonHandler());
areaTA=new JTextArea(12,35);
areaTA.setLineWrap(true);
areaTA.setWrapStyleWord(true);
areaTA.setEditable(true);
fileM=new JMenu("File");
exitMI=new JMenuItem("Exit");
restartMI=new JMenuItem("Restart Test");
toCopyTA=new JTextArea("Text to be typed");
toCopyTA.setLineWrap(true);
toCopyTA.setWrapStyleWord(true);
toCopyTA.setEditable(false);
timeLBL=new JLabel("Time: ");
timeTF=new JTextField("");
timeTF.setEditable(false);
speedLBL=new JLabel("Words Per Minute: ");
speedTF=new JTextField();
speedTF.setEditable(false);
errorsLBL=new JLabel("Errors: ");
errorsTF=new JTextField();
errorsTF.setEditable(false);
blankLBL=new JLabel("");
fileM.add(exitMI);
fileM.add(restartMI);
Container pane=getContentPane();
pane.setLayout(new GridLayout(3, 2));
JPanel panel=new JPanel();
panel.add(toCopyTA, BorderLayout.CENTER);
pane.add(panel);
JPanel panel1=new JPanel(new FlowLayout());
panel1.add(timeLBL);
panel1.add(timeTF);
pane.add(panel1);
JPanel panel2=new JPanel();
panel2.add(areaTA, BorderLayout.CENTER);
pane.add(panel2);
JPanel panel3=new JPanel(new FlowLayout());
panel3.add(speedLBL);
panel3.add(speedTF);
pane.add(panel3);
JPanel panel5=new JPanel(new FlowLayout());
panel5.add(blankLBL);
pane.add(panel5);
JPanel panel4=new JPanel(new FlowLayout());
panel4.add(startBTN);
pane.add(panel4);
setTitle("Typing Test");
setSize(800, 625);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class startButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
//need to start countdown timer when button is clicked
//code below happens when the time equals 0
typed=areaTA.getText();
String[] words=typed.split(" ");
JOptionPane.showMessageDialog(null, "Your average speed: "+(words.length/60)+"/n Words typed: "+words.length);
}
}
public static void main(String[] args)
{
TypingTest run=new TypingTest();
}
}
Any advice on how to create a working timer would be greatly appreciated. I've been trying to figure this out for two days and have gotten nowhere.
Charles
Message was edited by:
CJ426
Updated code