/* * Clock1 - simplest possible clock using ClockTimer */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.GregorianCalendar; import java.util.GregorianCalendar; public class Clock1 extends JFrame implements GUIClock { private JTextField textF = null; private ClockTimer time; public Clock1() { draw(); } /* * Draw the UI initially. */ private void draw() { JFrame mainFrame = new JFrame("Clock1"); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // text field to display time textF = new JTextField(""); textF.setColumns(8); textF.setEditable(false); mainFrame.add(textF); // start the clock timer thread to update this clock new ClockTimer(this, 1).execute(); //Display the window. mainFrame.pack(); mainFrame.setVisible(true); } // draw() // update is called from the ClockTimer.process() by event dispatcher thread public void update(GregorianCalendar t) { String ts = t.get(t.HOUR) + ":" + t.get(t.MINUTE) + ":" + t.get(t.SECOND); textF.setText(ts); } /* * Start the gui. */ public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { Clock1 cgui = new Clock1(); } }); } // main() } // class Clock1