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 {     /** ...

Get Current System Time & Date - Netbeans Java Code

Run Shot of Current System Time & Date Java Code: import java.text.SimpleDateFormat; import java.util.Date; public class Datetimemonth { public static void main(String[] args) { Date now = new Date(); System.out.println("toString(): " + now); // dow mon dd hh:mm:ss zzz yyyy // SimpleDateFormat can be used to control the date/time display format: // E (day of week): 3E or fewer (in text xxx), >3E (in full text) // M (month): M (in number), MM (in number with leading zero) // 3M: (in text xxx), >3M: (in full text full) // h (hour): h, hh (with leading zero) // m (minute) // s (second) // a (AM/PM) // H (hour in 0 to 23) // z (time zone) SimpleDateFormat dateFormatter = new SimpleDateFormat("E, y-M-d 'at' h:m:s a z"); System.out.println("Format 1: " + dateFormatter.format(now)); dateFormatter = new Sim...

व्हाट्सएप पेमेंट को शुरू कैसे करें [वीडियो अंदर है]

  इस पोस्ट में, मैं आपको बताऊंगा कि अपने एंड्रॉइड फोन (android phone) में व्हाट्सएप भुगतान (WhatsApp Payments) कैसे सक्रिय करें।   खराब नेटवर्क में भी इस पृष्ठ को लोड करने में तेजी लाने के लिए, यहां कोई चित्र नहीं दिखाया जाएगा।  नीचे दिए गए चरणों का पालन करें: 1.      सबसे पहले, हमें यह जांचने की आवश्यकता है कि आपने अपने फोन पर व्हाट्सएप (WhatsApp) का नया संस्करण स्थापित किया है या नहीं। जांच करने के लिए, अपने फोन में प्लेस्टोर (Playstore) खोलें और व्हाट्सएप (WhatsApp) खोजें। व्हाट्सएप (WhatsApp) आइकन पर क्लिक करें और इस ऐप के बारे में क्लिक करें। यदि आप अपडेट (Update) बटन देखते हैं, तो आपको व्हाट्सएप (WhatsApp) को अपडेट करने के लिए अपडेट बटन को दबाने की आवश्यकता है और यदि आप अपडेट बटन नहीं देखते हैं, तो इसका मतलब है कि आपके पास पहले से ही आपके फोन में इंस्टॉल (install) किया गया एप्लिकेशन (application) का नवीनतम संस्करण (newest version) है। 2.      अब अपने फोन में व्हाट्सएप एप खोलें। ऐप के होमस्क्रीन (homescreen) पर, तीन डॉट्स (three dots) प...