-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInnerClassTest.java
63 lines (54 loc) · 1.49 KB
/
InnerClassTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package innerClass;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
/**
* This program demonstrates the use of inner classes,
*
* @author Cay Horstmann
* @version 1.11 2015-05-12
*/
public class InnerClassTest {
public static void main(String[] args) {
TalkingClock clock = new TalkingClock(1000, true);
clock.start();
// keep program running until user selects "0k"
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
}
/**
* A clock that prints the time in regular intervals.
*/
class TalkingClock {
private final int interval;
private final boolean beep;
/**
* Constructs a talking clock
*
* @param interval the interval between messages (in milliseconds)
* @param beep true if the clock should beep
*/
public TalkingClock(int interval, boolean beep) {
this.interval = interval;
this.beep = beep;
}
/**
* Starts the clock.
*/
public void start() {
ActionListener listener = new TimePrinter();
Timer t = new Timer(interval, listener);
t.start();
}
private class TimePrinter implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("At the tone, the time is " + new Date());
if (beep)
Toolkit.getDefaultToolkit().beep();
}
}
}