Skip to main content

Digital Clock - Netbeans Java Code

Run-Shot of Digital Clock

Java Code:

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);
 }
}

You can also download text file version of this code: DigitalClock.txt


Comments

Popular posts from this blog

Javax Code for Playing Music:

Javax Code for Playing Music: OUTPUT: ________________________________________________________________ Just Copy and Paste and Change the variable name respectively: ________________________________________________________________ ________________________________________________________________ Buttons: 1. jButton1 : Open Music File 2. jButton2 : Play 3, jButton3 : Stop 4. jButton4 : Pause ________________________________________________________________ import java.io.File;     import java.io.IOException;     import java.net. MalformedURLException;     import javax.sound.sampled. AudioInputStream;     import javax.sound.sampled. AudioSystem;     import javax.sound.sampled.Clip;     import javax.sound.sampled. LineUnavailableException;     import javax.sound.sampled. UnsupportedAudioFileException; public class Music extends javax.swing.JFrame {     /** ...

My Educational Philosophy in a Nutshell - Hareshwar P. Singh

My Educational Philosophy in a Nutshell Author: Hareshwar P Singh Education, in a wider perspective, is not merely the accumulation of knowledge and information in various academic disciplines, which is nevertheless essential in order to earn one’s living and survive in an increasingly competitive world. In its holistic sense, education is a means of total development of a pupil—physical, intellectual and emotional/spiritual development. True education must lead one from knowledge to wisdom, and ultimately to a profound sense of intelligence and enlightenment. Such an educated person has a great insight not only into the way human mind works but also into the ways of the world, and therefore is capable of living sanely in this seemingly insane world. Taking a more pragmatic view, it is the function or role of education to keep society supplied with various kinds of skilled professionals needed for the smooth functioning of a nation. In the contemporary educational landscape, ...

Get Pascal's Triangle - Netbeans Java Codes

Run-Shot of Pascal's Triangle Java Code: public class Pascalstriangle { public static void main(String args[]) { int x = 6; int triangle[][] = new int[x][x]; for (int i = 0; i < x; i++) { for (int j = 0; j < x; j++) { triangle[i][j] = 0; } } for(int i = 0; i < x; i++) { triangle[i][0] = 1 ; } for (int i = 1; i < x; i++) { for (int j = 1; j < x; j++) { triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]; } } for (int i = 0; i < x; i++) { for(int j=0;j<=i;j++) { System.out.print(triangle[i][j]+ " "); } System.out.println(); } } } [Find more Netbeans Java Code at www.sumikuma.tk] You can also download text file version of this code: Pascal-triangle_javacode.txt