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);
}
}
运行结果:
C:\>java Test
at the tone,the time isSun Oct 29 13:40:25 CST 2006
OK
at the tone,the time isSun Oct 29 13:40:25 CST 2006
OK
at the tone,the time isSun Oct 29 13:40:26 CST 2006
OK
at the tone,the time isSun Oct 29 13:40:27 CST 2006
OK
at the tone,the time isSun Oct 29 13:40:28 CST 2006
OK
at the tone,the time isSun Oct 29 13:40:29 CST 2006
OK
at the tone,the time isSun Oct 29 13:40:30 CST 2006
OK
at the tone,the time isSun Oct 29 13:40:31 CST 2006
OK
at the tone,the time isSun Oct 29 13:40:32 CST 2006
OK
java Timer的应用
最新推荐文章于 2024-08-27 20:00:00 发布
本文展示了一个使用Java Swing和AWT实现的简单定时任务示例。该程序每隔一秒执行一次任务,显示当前时间,并通过控制台输出。此外,程序利用了Swing的消息对话框来提示用户程序结束。
882

被折叠的 条评论
为什么被折叠?



