1 /*
2 * 本例子实现java中的内部类
3 * 功能:定时打印字符串,并beep
4 * 内部类TimerPrinter implements ActionListener
5 */
6 import java.awt.*;
7 import java.awt.event.*;
8 import java.util.*;
9
10 import javax.swing.*;
11 import javax.swing.Timer;
12
13
14
15 //to resolve conflict with java.util.Timer;
16 public class InnerClassTest {
17 public static void main(String[] args)
18 {
19 TalkingClock clock = new TalkingClock(1000,true);
20 clock.start();
21 //keep program running until user selects "OK"
22 JOptionPane.showConfirmDialog(null, "Quti program?");
23 System.exit(0);
24 }
25
26 }
27 class TalkingClock
28 {
29 public TalkingClock(int interval,boolean beep)
30 {
31 this.interval = interval;
32 this.beep = beep;
33 }
34
35 public void start()
36 {
37 ActionListener listener = new TimerPrinter();
38 Timer t = new Timer(interval,listener);
39 t.start();
40 }
41
42 private int interval;
43 private boolean beep;
44
45 public class TimerPrinter implements ActionListener {
46 public void actionPerformed(ActionEvent event)
47 {
48 Date now = new Date();
49 System.out.println("At the tone,the time is"+now);
50 Toolkit.getDefaultToolkit().beep();
51 }
52
53 }
54 }
posted on 2011-04-17 19:13  aodixius  阅读(532)  评论(0编辑  收藏  举报