public class TimerTester
1: import java.awt.*;
2: import java.awt.event.*;
3: import java.util.*;
4: import javax.swing.*;
5: import javax.swing.Timer;
7: /**
8: This program shows a clock that is updated once per second.
9: */
10: public class TimerTester
11: {
12: public static void main(String[] args)
13: {
14: JFrame frame = new JFrame();
16: final int FIELD_WIDTH = 20;
17: final JTextField textField = new JTextField(FIELD_WIDTH);
19: frame.setLayout(new FlowLayout());
20: frame.add(textField);
22: ActionListener listener = new
23: ActionListener()
24: {
25: public void actionPerformed(ActionEvent event)
26: {
27: Date now = new Date();
28: textField.setText(now.toString());
29: }
30: };
31: final int DELAY = 1000;
32: // Milliseconds between timer ticks
33: Timer t = new Timer(DELAY, listener);
34: t.start();
36: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
37: frame.pack();
38: frame.setVisible(true);
39: }
40: }