以前写的例子 用了这个类
import java.util.*;
import java.awt.*;
import java.text.*;
import javax.swing.*;
class ClockPanel extends JPanel implements Runnable{
Thread th=null;
String today;
Date date=null;
GregorianCalendar cal=new GregorianCalendar();
SimpleDateFormat df=new SimpleDateFormat("yyyy MM dd HH:mm:ss");
public ClockPanel(){
th=new Thread(this);
th.start();
}
public void run(){
while(th!=null){
repaint();
try{
Thread.sleep(500);
}catch(InterruptedException e){}
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
date=new Date();
cal.setTime(date);
today=df.format(date);
g.drawString(today,80,80);
}
}
class ClockFrame extends JFrame{
public ClockFrame(){
Container con=getContentPane();
ClockPanel p=new ClockPanel();
setTitle("clock");
setSize(300,200);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con.add(p);
}
}
public class MyClock{
public static void main(String args[]){
ClockFrame f=new ClockFrame();
f.setVisible(true);
}
}