import java.util.*;
import java.awt.*;
import javax.swing.*;
public class FourTimer extends JFrame
{
Watch watch=new Watch();
public FourTimer()
{
super("时间显示");
setSize(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pan=new JPanel();
pan.setLayout(new GridLayout(1,1,15,15));
pan.add(watch);
setContentPane(pan);
}
public static void main(String[] args)
{
JFrame time=new FourTimer();
time.pack();
time.setVisible(true);
}
}
class Watch extends JPanel implements Runnable
{
Thread runner;
Watch()
{
if(runner==null)
{
runner=new Thread(this);
runner.start();
}
}
public void run()
{
while(true)
{
repaint();
try {
Thread.sleep(1000);
}
catch (InterruptedException ex) {
}
}
}
public void paintComponent(Graphics ss)
{
Graphics2D ss2D=(Graphics2D)ss;
Font type=new Font("Serif",Font.BOLD,12);
ss2D.setFont(type);
ss2D.setColor(getBackground());
ss2D.fillRect(0,0,getSize().width,getSize().height);
GregorianCalendar day=new GregorianCalendar();
String time=day.getTime().toString();
ss2D.setColor(Color.red);
ss2D.drawString(time,5,25);
}
}
如果用上面的就会显示出这样的图形:
如果是 用show();就正常.
但在JDK1.6中不能用show();只有用pack();setVisibel();,这样一来就显示不正常了,还是我的代码有问题??