import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Date; public class TimePrinter implements ActionListener { public void actionPerformed(ActionEvent event){ System.out.println("现在的时间是"+new Date()); Toolkit.getDefaultToolkit().beep(); //发出一声铃响 } }
import javax.swing.Timer; import java.awt.event.ActionListener; public class Main { public static void main(String[] args){ ActionListener listener = new TimePrinter(); Timer t = new Timer(10000,listener);//10秒跑一次 t.start();//启动定时器 } }