import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.text.SimpleDateFormat;
class Clock extends Frame implements Runnable{
Label time;
Thread timer;
volatile boolean running;
SimpleDateFormat timeFormat;
Date startDate=new Date();
boolean hasStart=false;
public Clock()
{
time=new Label();
time.setFont(new Font("Times New Roman",Font.BOLD,60));
time.setAlignment(Label.CENTER);
this.setLayout(new BorderLayout());
this.add(time,BorderLayout.CENTER);
timeFormat=new SimpleDateFormat("mmm:ss:SS");
time.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if (e.getButton()==MouseEvent.BUTTON1)
start();
else stop();
}
});
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void start()
{
if(time!=null)time=null;
startDate=new Date();
timer=new Thread(this);
running=true;
timer.start();
}
public void run(){
while(running){
time.setText(timeFormat.format(new Date().getTime()-startDate.getTime()));
try{
Thread.sleep(50);
}
catch(InterruptedException e){}
}
}
public void stop(){
running=false;
}
public static void main(String[] args){
Clock c=new Clock();
c.setBounds(0,0,350,100);
c.setAlwaysOnTop(true);
c.setResizable(false);
c.setVisible(true);
}
}
import java.awt.event.*;
import java.util.Date;
import java.text.SimpleDateFormat;
class Clock extends Frame implements Runnable{
Label time;
Thread timer;
volatile boolean running;
SimpleDateFormat timeFormat;
Date startDate=new Date();
boolean hasStart=false;
public Clock()
{
time=new Label();
time.setFont(new Font("Times New Roman",Font.BOLD,60));
time.setAlignment(Label.CENTER);
this.setLayout(new BorderLayout());
this.add(time,BorderLayout.CENTER);
timeFormat=new SimpleDateFormat("mmm:ss:SS");
time.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if (e.getButton()==MouseEvent.BUTTON1)
start();
else stop();
}
});
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void start()
{
if(time!=null)time=null;
startDate=new Date();
timer=new Thread(this);
running=true;
timer.start();
}
public void run(){
while(running){
time.setText(timeFormat.format(new Date().getTime()-startDate.getTime()));
try{
Thread.sleep(50);
}
catch(InterruptedException e){}
}
}
public void stop(){
running=false;
}
public static void main(String[] args){
Clock c=new Clock();
c.setBounds(0,0,350,100);
c.setAlwaysOnTop(true);
c.setResizable(false);
c.setVisible(true);
}
}