import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DateFormat;
import java.util.Date;
public class Test5 {
public static void main(String arg[]) {
Frame f=new Frame("时钟");
Panel p=new Panel();
Label l=new Label("显示时钟");
myThread mt=new myThread(l);
l.setText(mt.showi());
p.add(l);
f.add(p);
f.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
f.setLocation(200, 30);
f.setSize(200, 80);
f.setVisible(true);
mt.start();
}
}
class myThread extends Thread {
private Label label;
public myThread(Label lab){
this.label=lab;
}
public String showi(){
Date date = new Date();
int year = date.getYear() + 1900;
int yue = date.getMonth() + 1;
int re = date.getDate();
int hh = date.getHours();
int dd = date.getMinutes();
int ss = date.getSeconds();
return year + "年" + yue + "月" + re + "日\t" + hh + ":"
+ dd + ":" + ss;// 得到日期
}
public void run() {
while (true) {
label.setText(showi());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}