Ask Questions?

View Latest Questions


 
 

Text Clock 2
Posted on: July 22, 2006 at 12:00 AM
This is the same as Example - Text Clock, but defines a reusable component for the clock.

Java: Example - Text Clock 2

In this page we will show you the demo of the Text clock.

We have embedded the applet in the web page for easy understanding of the code.

 

If you browser is Java enabled then you should be able to see the running text clock.

 

This is the same as Example - Text Clock, but defines a reusable component for the clock.

 

See javax.swing.Timer for an explanation of a simple timer class.

 

You should be able to see the following applet clock:

 

Main program / Applet

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
// File:   textclock/TextClock2.java
// Description: Application / Applet for a simple text clock.
// Author: Fred Swartz
// Date:   2005-02-15, 1999-05-01, 2001-11-02, 2002-11-07

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

///////////////////////////////////////////////////////////// TextClock2
public class TextClock2 extends JApplet {

    //====================================================== constructor
    public TextClock2() {
        add(new Clock());
    }

    //============================================================= main
    public static void main(String[] args) {
        JFrame window = new JFrame("Time of Day");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        window.setContentPane(new Clock());

        window.pack();
        window.setVisible(true);
        System.out.println(window.getContentPane().size());
    }
}

Clock component

This text clock component is created by subclassing JTextField. Whether thi