import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Timer implements ActionListener
{
public static int time;
//containers
private JFrame f;
private JButton start, stop, reset;
//components
private JLabel hour, min, sec;
private JTextField tf1, tf2, tf3;
private JPanel p1, p2;
public Timer()
{
//containers
f = new JFrame("Rad's Timer");
p1 = new JPanel();
p2 = new JPanel();
//components
start = new JButton("Start");
stop = new JButton("Stop");
reset = new JButton("Reset");
hour = new JLabel("Hour: ");
min = new JLabel("Minute: ");
sec = new JLabel("Second: ");
tf1 = new JTextField("0", 7);
tf2 = new JTextField("0", 7);
tf3 = new JTextField("0", 7);
}
public void launchFrame()
{
p1.add(hour);
p1.add(tf1);
p1.add(min);
p1.add(tf2);
p1.add(sec);
p1.add(tf3);
p2.add(start);
p2.add(stop);
p2.add(reset);
p2.setLayout(new GridLayout());
f.setLayout(new BorderLayout(2,1));
f.add(p1, BorderLayout.NORTH);
f.add(p2, BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
//event handlers registration
start.addActionListener(this);
reset.addActionListener(this);
f.addWindowListener(new MyCloseButtonHandler());
}
public void actionPerformed(ActionEvent ae)
{
Object source = ae.getSource();
int num1, num2, num3;
if (tf1.getText() != null && tf2.getText() != null && tf3.getText() != null)
{
/*num1 = Integer.parseInt(tf1.getText());
num2 = Integer.parseInt(tf2.getText());*/
num3 = Integer.parseInt(tf3.getText());
if (source == start)
{
for (int i = time; i >= 0; i--)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException x) {}
tf3.setText(Integer.toString(num3));
}
}
if (source == reset)
{
tf1.setText("0");
tf2.setText("0");
tf3.setText("0");
}
}
}
/*public void count()
{
for (int i = time; i >= 0; i--)
{
while (i > 0)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException x) {}
tf3.setText("Time: " + i--);
}
}
}*/
private class MyCloseButtonHandler extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
public static void main(String args[])
{
Timer t = new Timer();
t.launchFrame();
}
}
I need help getting the start button to work on my code. so when you click start, the number you input on TextField for seconds will start a countdown.