1 |
import java.awt.event.ActionEvent; |
2 |
import java.awt.event.ActionListener; |
3 |
import javax.swing.JOptionPane; |
4 |
import javax.swing.Timer; |
5 |
|
6 |
/** |
7 |
This program tests the Timer class. |
8 |
*/ |
9 |
public class TimerTest |
10 |
{ |
11 |
public static void main(String[] args) |
12 |
{ |
13 |
class CountDown implements ActionListener |
14 |
{ |
15 |
public CountDown(int initialCount) |
16 |
{ |
17 |
count = initialCount; |
18 |
} |
19 |
|
20 |
public void actionPerformed(ActionEvent event) |
21 |
{ |
22 |
if (count >= 0) |
23 |
System.out.println(count); |
24 |
if (count == 0) |
25 |
System.out.println("Liftoff!"); |
26 |
count--; |
27 |
} |
28 |
|
29 |
private int count; |
30 |
} |
31 |
|
32 |
CountDown listener = new CountDown(10); |
33 |
|
34 |
final int DELAY = 1000; // milliseconds between timer ticks |
35 |
Timer t = new Timer(DELAY, listener); |
36 |
t.start(); |
37 |
|
38 |
JOptionPane.showMessageDialog(null, "Quit?"); |
39 |
System.exit(0); |
40 |
} |
41 |
} |