// TextClock.java -- Uses Timer, Calendar, JTextField.
//  -- Fred Swartz, 1999-05-01, 2001-11-02
//    -- Bary W Pollack, 2007-02-01
//  Enhancements: center, 12 hour, alarm, timer.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;      // Only need this one class

////////////////////////////////////////////////////////////////// TextClock
public class TextClock {
    //================================================================= main
    public static void main(String[] args) {
        JFrame clock = new TextClockWindow();
        clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        clock.setLocation(375, 200);
        clock.setVisible(true);
    } // end main()
} // end class TextClock


//////////////////////////////////////////////////////////// TextClockWindow
class TextClockWindow extends JFrame {
    //=================================================== Instance variables
    private JTextField jtfTimeField;  // Set by timer listener

    //========================================================== Constructor
    public TextClockWindow() {
        super("Text Clock");
        // Build the GUI - only one panel
        jtfTimeField = new JTextField(5);
        jtfTimeField.setFont(new Font("sansserif", Font.PLAIN, 48));
        jtfTimeField.setEditable(false);

        Container content = getContentPane();
        content.setLayout(new FlowLayout());
        content.add(jtfTimeField); 
        pack();
        setResizable(false);

        // Create a 1-second timer and action listener for it.
        Timer t = new Timer(1000,
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Calendar now = Calendar.getInstance();
                    int h = now.get(Calendar.HOUR_OF_DAY);
                    int m = now.get(Calendar.MINUTE);
                    int s = now.get(Calendar.SECOND);
                    jtfTimeField.setText(String.format("%2d:%02d:%02d", h, m, s));
                }
            });
        t.start();  // Start the timer
    } // end constructor
} // end class TextClock
