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);
}
}
----------------解决方案--------------------------------------------------------
package cgs;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.text.SimpleDateFormat;
class Clock extends JPanel implements Runnable{
private JTextField jt;
private String date="";
private int x;
private SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE MM月dd日 yyyy年 HH:mm:ss");
public Clock(){
setOpaque(false);
Thread c=new Thread(this);
c.start();
setLayout(new BorderLayout());
jt=new JTextField();
jt.setEditable(false);
jt.setHorizontalAlignment(JTextField.CENTER);
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){
g.drawString(date,x,42);
Date date=new Date() ;
jt.setText((bartDateFormat.format(date)));
}
}
public class ImageBackground extends JPanel{
URL url=null;
Image image=null;
public ImageBackground(){
image = (new ImageIcon("2.jpg")).getImage();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(image,0,0,getWidth(),getHeight(),this);
}
public static void main(String args[]){
Clock cd=new Clock();
ImageBackground ab=new ImageBackground();
JFrame j=new JFrame("我的时钟");
ab.setLayout(new BorderLayout());
ab.add(cd);
j.getContentPane().add(ab);
j.setBounds(200,200,400,400);
j.setVisible(true);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
[此贴子已经被作者于2007-1-9 19:06:09编辑过]
----------------解决方案--------------------------------------------------------
可以显示丫
----------------解决方案--------------------------------------------------------
有何异常
----------------解决方案--------------------------------------------------------