import java.awt.Frame; import java.awt.HeadlessException; import java.awt.Label; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.text.SimpleDateFormat; import java.util.Date; public class Timer { public static void main(String[] args) { Time time = new Time("系统时间"); Thread t = new Thread(time); t.start(); } } class Time extends Frame implements Runnable { Date nowtime; Label text; Label timeStr; Time(String str) { super(str); // TODO Auto-generated constructor stub text = new Label("现在的时间是:", text.CENTER); nowtime = new Date(); SimpleDateFormat matter1 = new SimpleDateFormat( "HH 时 mm 分 ss秒 yyyy年 MM 月DD 日 E"); timeStr = new Label(matter1.format(nowtime)); add(text); add(timeStr); // setBounds(x,y,width,height); x:组件在容器X轴上的起点 y:组件在容器Y轴上的起点 组件的长度 // height:组件的高度 setBounds(100, 100, 500, 150); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); validate(); } @Override public void run() { while (true) { nowtime = new Date(); SimpleDateFormat matter2 = new SimpleDateFormat( "HH 时 mm 分 ss秒 yyyy年 MM 月DD 日 E"); timeStr.setText((matter2.format(nowtime))); try { Thread.sleep(1000); } catch (Exception ex) { } } } }