Java Reference
In-Depth Information
*tocreate a digital clock. **/
public class ClockTimer1 extends Japplet
{
java.util.Timer fTimer;
// Need panel reference in run().
DateFormatPanel fClockPanel;
public void init () {
Container content - pane = getContentPane ();
// Create an instance of DrawingPanel
fClockPanel = new DateFormatPanel ();
// Add the DrawingPanel to the contentPane.
content - pane.add (fClockPanel);
}
public void start () {
// Create a timer.
fTimer = new java.util.Timer ();
// Start the timer immediately and then repeat calls
// to run in UpdateTask object every second.
fTimer.schedule (new UpdateTask (), 0, 1000);
}
/** Stop clock when web page unloaded. **/
public void stop () {
// Stop the clock updates.
fTimer.cancel ();
}
/** Use the inner class technique to define the
* TimerTask subclass to update the clock.**/
class UpdateTask extends java.util.TimerTask {
public void run () {
fClockPanel.repaint ();
}
}
} // class ClockTimer1
(Note that since we import both javax.swing.* and java.util.* it is
necessary to use the fully qualified type java.util.Timer when declaring the
fTimer variable. Without the full qualification, the compiler would not know
whether we wanted javax.swing.Timer or java.util.Timer .)
 
Search WWH ::




Custom Search