程序如下:
import java.awt.*;
import java.util.Date;
import java.applet.*;public class Clock extends applet implements Runnable
{
Graphics g;
Font font;
FontMetrics font_metrics;
Thread my_thread;
int width;
int height;
Date current_time;
public void init(){
g=getGraphics();
current_time=new Date();
set_defaults();
  }
void set_defaults(){
width=size().width;
height=size().height;
if(width>height*6)width=6*height;
else height=width/6;
font=new Font("TimesRoman",Font.BOLD,height*3/2);
g.setFont(font);
font_metrics=g.getFontMetrics();
  }
public void start(){
if(my_thread==null){
   my_thread=new Thread(this);
   my_thread.start();
   }
  }
public void stop(){
   my_thread.stop();
   my_thread==null;
  }
public void run(){
while(my_thread!=null){
   repaint();
   try{
       Thread.sleep(1000);
      }
   catch(InterruptedException e)
   }
  }
public void update(Graphics g){
g.setColor(Color.lightGray);
g.fillRect(0,0,width,height);
current_time=new Date();
int hours=current_time.getHours();
int minutes=current_time.getMinutes();
int seconds=current_time.getSeconds();
String time;
if(hours<10)time="0"+hours;
else time=""+hours;
if(minutes<10)time=":0"+minutes;
else time=":"+minutes;
if(seconds<10)time=":0"+seconds;
else time=":"+seconds;
g.setColor(Color.black);
g.setFont(font);
g.drawString(time,-1,height+5);
   }
public void paint(Graphics g){
Set_defaults();
update(g);
   }
}