package itat;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;
import javax.swing.Timer;
public class Example9_18 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TimeWin Win=new TimeWin();
}
}
class TimeWin extends Frame implements ActionListener{
TextField text;
Button bStart,bStop,bContinue;
Timer time;
int n=0,start=1;
TimeWin(){
time=new Timer(1000,this);
text=new TextField(10);
bStart=new Button("开始计时");
bStop=new Button("暂停计时");
bContinue=new Button("继续计时");
bStart.addActionListener(this);
bStop.addActionListener(this);
bContinue.addActionListener(this);
setLayout(new FlowLayout());
add(bStart);
add(bStop);
add(bContinue);
add(text);
setSize(500,500);
validate();
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==time){
Date date=new Date();
String str=date.toString().substring(11,19);
text.setText("时间:"+str);
int x=text.getBounds().x;
int y=text.getBounds().y;
y=y+2;
text.setLocation(x,y);
}else if(e.getSource()==bStart){
time.start();
}else if(e.getSource()==bStop){
time.stop();
}else if(e.getSource()==bContinue){
time.restart();
}
}
}