package diliuzhang;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Timer;
import javax.swing.JOptionPane;
public class InnerClassTest {
public static void main(String[] args) {
TalkingClock clock =new TalkingClock(1000,true);
clock.start();
JOptionPane.showMessageDialog(null,"Quit program?");
System.exit(0);
}
}
class TalkingClock{
private int interval;
private boolean beep;
public TalkingClock(int interval,boolean beep) {
this.interval=interval;
this.beep=beep;
}
public void start() {
ActionListener listener=new TimerPrinter();
//构造一个定时器 每个xx毫秒通知 listener一次
Timer t=new Timer(interval,listener);
//启动
t.start();
}
public class TimerPrinter implements ActionListener{
public void actionPerformed(ActionEvent event) {
System.out.println("At the tone,the time is"+new Date());
if(beep)Toolkit.getDefaultToolkit().beep();
}
}
}
慕瓜4170288
慕瓜4170288
相关分类