Saturday, April 20, 2013

Clock program


The Text Clock is a Java program to display a text-based clock to show the time progress. To get the current date and time, the  program uses Date class. To display  AM or PM along with the time, the Date object  needs to be formatted by using the SimpleDateFormat class. To make a real clock, the Timer class is used. The Timer class is able to do its task in a specified interval of time so that the progress of time can be shown.

Clock source code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Date;
import java.text.SimpleDateFormat;
class  Clock extends JFrame{
            JLabel lblshow;
           
            Clock(){
1                      setDefaultCloseOperation(EXIT_ON_CLOSE);
2                      setTitle("Text Clock");
3                      setSize(100,70);
4                      lblshow=new JLabel("Current Time");
5                      lblshow.setForeground(Color.CYAN);
6                      Containter cont=getContentPane();                                
7                      cont.add(lblshow, BorderLayout.CENTER);
8                      cont.setBackground(Color.BLACK);
9                      setVisible(true);       
                        }

10        public void start(){
                        timer = new Timer(1000,new AListener());
                        timer.start();
             }

11        class AListener implements ActionListener{
                        public void actionPerformed( ActionEvent e ) {
                                    Date dat=new Date();
                                    String ftime="HH:mm:ss a";
                                    SimpleDateFormat fd = new SimpleDateFormat(ftime);
                                    lblshow.setText(fd.format(dat));
                                                }          
                        }
           
}

12 public class TextClock{
 
            public static void main(String args[]){
                        Clock c=new Clock();
                        c.start();
            }


}




Code Explanation:
1 Close the program window when the close button is pushed.
2 Specify the title of the program window (Text Clock).
3 Specify the size of the program window to open (width=100, height=70).
4 Create the JLabel lblshow object. This label object is to display the time progress.
5 Specify the text color of the lblshow object (CYAN).
6 Create the Container cont object. The Container object is the workspace to place other components. In this program, only lblshow object is placed on the Container cont object.
7 Place the lblshow object on the Container cont.
8 Specify the background color of the Container cont (BLACK).
9 Make sure the program window is visible.
10 The start method has code to show time and its progress. The Timer timer object is created to show the time progress. The Timer class constructor is Timer(int delay, ActionListner). In this program, we want to show the time progress in each every second. So, the delay is  1000 milliseconds. The ActionListener has the actionPerformed method that needs to be implemented to enable the timer to do action in every second.
11 The AListener class implements the actionPerformed method of the ActionLister interface. When the timer performs its action in every second, the Date object is created and formatted by using the SimpleDateFormat class to show only the time (with AM or PM). The time progress is shown on the lblshow by using setText(string) method.
12 The TextClock class the main method to start showing the clock.

2 comments:

  1. I loved as much as you will receive carried out right here. The sketch is attractive, your authored material stylish. nonetheless, you command get bought an impatience over that you wish be delivering the following. unwell unquestionably come more formerly again as exactly the same nearly a lot often inside case you shield this hike. BrowSEO

    ReplyDelete
  2. Excellent blog here! Also your web site loads up very fast! What web host are you using? Can I get your affiliate link to your host? I wish my site loaded up as quickly as yours lol. teeth whitening Singapore price

    ReplyDelete