import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.text.*;
import java.text.SimpleDateFormat;
class Clock extends JPanel implements Runnable{
private JTextField jt;
private JPanel jp=new JPanel();
private String date="";
private int x;
private SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE MM月dd日 yyyy年 HH:mm:ss");
public Clock(){
Thread c=new Thread(this);
c.start();
jt=new JTextField();
jt.setEditable(false);
jt.setHorizontalAlignment(jt.CENTER);
this.setLayout(new BorderLayout());
add(jt,BorderLayout.SOUTH);
}
public void run(){
while(true){
try{
Thread.sleep(50);
date=bartDateFormat.format(new Date());
x+=2;
if(x>this.getWidth()){
x=0;
}
repaint();
} catch(Exception e){
e.printStackTrace();
}
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString(date,x,42);
Date date=new Date() ;
jt.setText((bartDateFormat.format(date)));
}
}
class ImageBackground extends JPanel{
URL url=null;
Image image=null;
public ImageBackground(){
url=this.getClass().getResource("2.jpg");
Toolkit toolkit=Toolkit.getDefaultToolkit();
image=toolkit.getImage(url);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(image,0,0,this);
}
public static void main(String args[]){
Clock cd=new Clock();
ImageBackground ab=new ImageBackground();
JFrame j=new JFrame("我的时钟");
j.getContentPane().add(ab.add(cd));
j.setBounds(200,200,400,400);
j.setVisible(true);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}