关于java编写的数字时钟timer不能刷新的问题

mmbulaila 2011-05-22 06:53:35
这是我的panel类,用来描绘数据时钟的信息呈现的
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.awt.Graphics;
public class TimerPane extends JPanel
{
private Date s3=new Date();
private Timer s2;
public TimerPane()
{
}
public void StarTime()
{
if ( s2 == null )
{
s2=new Timer(1000,new TimerHandler());
s2.start();
}
else
{
if ( ! s2.isRunning() )
s2.restart();
}

}
public void paintComponent(Graphics g)
{
super.paintComponent(g);

SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");

g.drawString((String)formatter.format(s3) ,100, 300);
}
private class TimerHandler implements ActionListener
{

public void actionPerformed( ActionEvent actionEvent )
{
repaint();
}
}
}
这是我的main类
import javax.swing.JFrame;
public class Framebox {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TimerPane s1=new TimerPane();
JFrame s2=new JFrame("Digital Clock");
s2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s2.setSize(300,600);
s2.setVisible(true);
s2.add(s1);
s1.StarTime();
}

}
想问问为什么时钟刷新不了,时钟都是一个数字
...全文
136 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ALLSTAR123 2011-05-24
  • 打赏
  • 举报
回复
可以将Date设置成局部变量就可以了

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧