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

Captcha - Netbeans Java Codes

You can also download text file of this code here:   Captcha.txt Java Code: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // [Find More Java Codes at www.sumikuma.tk] if ("Cleven Eleven".equals(txt1.getText())) JOptionPane.showMessageDialog(this,"You answer is correct"); else JOptionPane.showMessageDialog(null, "You are unsuccessful, Please Try Again"); } How To Display Image? : jLabel1>Properties>Icon>(Under External Image) Import to Image>Select Suitable Image from File Chooser>Select Target Folder>Click Ok> Close the Properties Window. [Find More Java Codes at www.sumikuma.tk]

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

Tic Tac Toe - Netbeans Java Codes

Java Code: public class TicTacToe implements ActionListener { final String VERSION = "1.0"; //Setting up ALL the variables[Find More Java Codes at www.sumikuma.tk] JFrame window = new JFrame("Tic-Tac-Toe " + VERSION); JMenuBar mnuMain = new JMenuBar(); JMenuItem mnuNewGame = new JMenuItem("New Game"), mnuInstruction = new JMenuItem("Instructions"), mnuExit = new JMenuItem("Exit"), mnuAbout = new JMenuItem("About"); JButton btn1v1 = new JButton("Player vs Player"), btn1vCPU = new JButton("Player vs CPU"), btnBack = new JButton("<--back"); JButton btnEmpty[] = new JButton[10]; JPanel pnlNewGame = new JPanel(), pnlNorth = new JPanel(), pnlSouth = new JPanel(), pnlTop = new JPanel(), pnlBottom = new JPanel(), pnlPlayingField = new JPanel(); JLabel lblTitle = new JLabel("Tic-Tac-Toe"); JTextArea txtMessage = new JTextArea(); final int winCombo[][] = new int[][] { {1, 2, 3}, {...