一个java计时器,谁能帮标下注释?

来源:百度知道 编辑:UC知道 时间:2024/06/01 21:27:50
这是代码,如果谁能帮改成逐渐增加计时器更好~~
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class Test implements ActionListener{
public void actionPerformed (ActionEvent event){
Date now=new Date();
System.out.println("at the tone,the time is"+now);
//Toolkit.getDefaultToolkit().beep();
System.out.println("OK");
}

public static void main(String[] args){
Test tp=new Test();
Timer timer=new Timer(1000,tp);
timer.start();
JOptionPane.showMessageDialog(null,"quit");
System.exit(0);
}
}

public class Test implements ActionListener{
//按下按键时执行
public void actionPerformed (ActionEvent event){
Date now=new Date();
//新建日期
System.out.println("at the tone,the time is"+now);
//打印当前日期
//Toolkit.getDefaultToolkit().beep();
System.out.println("OK");
打印OK
}

public static void main(String[] args){
Test tp=new Test();
//新建一个默认构造的Test
Timer timer=new Timer(1000,tp);
//新建Timer每1000毫秒,执行tp。。。???why?
timer.start();
JOptionPane.showMessageDialog(null,"quit");
//显示消息框,父窗口为空,消息:quit
System.exit(0);
//退出虚拟机
}
}
执行结果是。。。弹出消息框,然后退出。。。
汗。。。监听没用到啊。。。

添加线程,在线程里,不停更新时间,可以确保时间的增加和不会出现误差,
1000毫秒,虽然是1秒,但是计算的过程中会出现误差。时间就了就能看出来,一小时会相差好几分钟。
线程代码如下
while(true){
System.out.println("at the tone,the time is"+new Date());
Thread.sleep(10