import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class MyThread extends Thread
{
JTextField tf;
MyThread(JTextField tf)
{
this.tf = tf;
}
public void run()
{
while(true)
{
Date d = new Date();
int h = d.getHours();
int m = d.getMinutes();
int s = d.getSeconds();
tf.setText(h+":"+m+":"+s);
try
{
Thread.sleep(300);
}
catch(Exception e){}
}
}
}
class Slip27_2 extends JFrame implements ActionListener
{
JTextField txtTime;
JButton btnStart,btnStop;
MyThread t;
Slip27_2()
{
txtTime = new JTextField(20);
btnStart = new JButton("Start");
btnStop = new JButton("Stop");
setTitle("Stop Watch");
setLayout(new FlowLayout());
setSize(300,100);
add(txtTime);
add(btnStart);
add(btnStop);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnStart.addActionListener(this);
btnStop.addActionListener(this);
t = new MyThread(txtTime);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btnStart)
{
if(!t.isAlive()) t.start();
else t.resume();
}
if(ae.getSource()==btnStop)
{
t.suspend();
}
}
public static void main(String a[])
{
new Slip27_2();
}
}