并在命令行窗口中每隔1秒显示一下当前时间,类似: At the tone, the time is Tue Dec 29 11:40:12 CST 2009 At the tone, the time is Tue Dec 29 11:40:13 CST 2009 At the tone, the time is Tue Dec 29 11:40:14 CST 2009 在单击“确定”按钮时自动退出程序。 源程序: import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.Timer; public class Java_3 public static void main(String[]args) ActionListener listener=new (1) (); Timer t=new Timer(1000, listener); t.start(); JOptionPane.showMessageDialog(null, "Quit program"); System.exit(0);
class TimePrinter (2) AetionListener public void actionPerformed(ActionEvent event) Date now=new Date(); System.out.println("At the tone, the time is"+now); Toolkit. getDefauhToolkit().beep();