Pages

Sunday, April 10, 2011

Create a Digital Clock in Java

Digital Clock in Java
Digital Clock
Download DigitalClock.java
Digital Clock class:
/**
 * @author Eri Setiawan
 */

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class DigitalClock extends JLabel {
 private String pattern;
 private Timer timer;
 private int delay;

 /**
  * Constructs a Digital Clock using the given pattern and the default delay.
  * @param pattern - the pattern describing the date and time format
  */
 public DigitalClock(String pattern){
  this.pattern = pattern;
  this.delay = 1000;
  createTimer();
  timer.start();
 }
 
 /**
  * Constructs a Digital Clock using the given pattern and delay.
  * @param delay - the number of milliseconds between action events
  * @param pattern - the pattern describing the date and time format
  */

 public DigitalClock(String pattern, int delay){
  this.pattern = pattern;
  this.delay = delay;
  createTimer();
  timer.start();
 }

 /**
  * Constructs a Digital Clock using the default pattern and delay.
  */

 public DigitalClock(){
  pattern = "hh:mm:ss a";
  this.delay = 1000;
  createTimer();
  timer.start();
 }

 private void createTimer(){
  timer = new Timer(delay, new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent arg0) {
    setText(new SimpleDateFormat(pattern).format(new Date()));
   }
  });
 }
 
 public String getPattern() {
  return pattern;
 }
 
 public void setPattern(String pattern) {
  this.pattern = pattern;
 }
 
 public Timer getTimer() {
  return timer;
 }
 
 public int getDelay() {
  return delay;
 }
 
 public void setDelay(int delay) {
  this.delay = delay;
 }
 
 public static void main(String[] args){
  JFrame frame = new JFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setBounds(100, 100, 193, 72);
  JPanel contentPane = new JPanel();
  frame.setContentPane(contentPane);
  contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
  DigitalClock digitalClock = new DigitalClock();
  contentPane.add(digitalClock);
  frame.setVisible(true);
 }
}

7 comments:

  1. how can I add this to an alredy created JFrame I have? I want to put this clock at top right of the main JPanel.

    ReplyDelete
    Replies
    1. ok we can see that JFrame in this example is built in void main so you can paste the code above before void main and then you set your JFrame that you want then call DigitalClock function there then add it to the pane.hoho.hopefully can help you!so sory for the long respons =(

      Delete
  2. gimana caranya saya mau bikin jam digital pake basic coding?

    ReplyDelete
  3. can anyone provide me code of digital clock program that has option of being used as stopwatch???

    ReplyDelete