Java 100-001:定时器程序

package java01;

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;

/**
 *   我的java每天100行代码001
 *  定时器程序:定时每隔10秒显示当前时间,并且响一次;采用了匿名内部类
 * @author Administrator
 *
 */
public class java001 {
	public static void main(String[] args) {
		TalkingClock clock = new TalkingClock();
		clock.start(10000,true);
		
		JOptionPane.showInternalMessageDialog(null, "Quit program");
		System.exit(0);
	}
}

class TalkingClock{
	
	
	public void start(int interval,final boolean beep){
		//匿名内部类:构造器+{  };
		ActionListener listener = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Date now = new Date();
				System.out.println("At the tone,the time is"+now);
				if(beep) Toolkit.getDefaultToolkit().beep();
			}
		};
		Timer t = new Timer(interval,listener);
		t.start();
	}
}

猜你喜欢

转载自blog.csdn.net/qq_43356439/article/details/85010339