Jpanel 上画图的问题
我想在一JPanel上画出一实时显示当前时间的字符串,用一线程画字符串,
代码如下:
class SimplePanel1 extends JPanel
{
SimpleDateFormat formatter;
java.util.Date currentDate;
String lastdate;
SimplePanel1()
{
super();
formatter=new SimpleDateFormat("EEE MMM dd hh:mm:ss yyyy",Locale.getDefault());
}
class DrawRunner implements Runnable
{
Graphics g;
DrawRunner(Graphics g)
{
this.g=g;
}
public void run()
{
while(true)
{
currentDate =new java.util.Date();
lastdate=formatter.format(currentDate);
g.drawString(lastdate,5,20);
currentDate=null;
try
{
Thread.sleep(300);
}
catch(InterruptedException e){}
}
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
DrawRunner drawrunner=new DrawRunner(g);
Thread DrawThread=new Thread(drawrunner);
DrawThread.start();
}
}
为什么Jpanel上的字符不会自动刷新,而且有时切换界面后字符串会消失呢?
请各位帮忙看看,是否还有其他方法实现?谢谢