package timer;
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 AnonymousInnerClass {
public static void main(String[] args) {
TalkingClock2 clock = new TalkingClock2();
clock.start(3000, true);
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
}
class TalkingClock2 {
public void start(int interval, final boolean beep) {
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
Date now = new Date();
System.out.println("At the tone, the time is : " + now);
if(beep) {
Toolkit.getDefaultToolkit().beep();
}
}
};
Timer timer = new Timer(interval, listener);
timer.start();
}
}
本文介绍了一个使用Java Swing Timer与匿名内部类实现的简单计时器程序。该程序每隔指定时间播报当前时间,并可根据设置发出提示音。
1万+

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



