JAVA-内部类例子

 
  
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 }

转载于:https://www.cnblogs.com/binbinjava/archive/2011/04/17/2019041.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值