给你个源代码你自己看看吧
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Clock extends Canvas implements Runnable{
/*
http://ohgrateboy.blog.我的博客
*/
private static final long serialVersionUID = 3660124045489727166L;
Thread t;
JFrame frame=new JFrame();
JPanel conPane;
String time;
int i=0;
Date timer;
public Clock(){
conPane=(JPanel)frame.getContentPane();
conPane.setLayout(new BorderLayout());
conPane.setSize(280,40);
conPane.setBackground(Color.white);
conPane.add(this,BorderLayout.CENTER);
t=new Thread(this);
//实例化线
t.start();
//调用线程
frame.setVisible(true);
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void run(){
while(true){
try{
Thread.sleep(1000);
//休眠1秒钟
}catch(InterruptedException e){
System.out.println("异常");
}
this.repaint(100);
}
}
public void paint(Graphics g){
Font f=new Font("宋体",Font.BOLD,16);
SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化时间显示类型
Calendar now=Calendar.getInstance();
time=SDF.format(now.getTime());
//得到当前日期和时间
g.setFont(f);
g.setColor(Color.orange);
g.drawString(time,45,25);
}
public static void main(String args[]){
new Clock();
}
}