ScaleFree

MAKING A DIGITAL CLOCK IN JAVA

Does making a Digital Clock in Java sounds cool to you? If yes then you, my friend definitely are in the place.

SOURCE CODE
  1.  
  2.  import java.awt.Font;
  3.  import java.awt.Color;
  4.  import java.awt.GridLayout;
  5.  import java.awt.event.ActionEvent;
  6.  import java.awt.event.ActionListener;
  7.  import javax.swing.JFrame;
  8.  import javax.swing.JLabel;
  9.  import javax.swing.Timer;
  10.  import javax.swing.SwingConstants;
  11.  import java.util.*;
  12.  import java.text.*;
  13.  
  14.  public class Clock 
  15.  {
  16.   public static void main(String[] args) 
  17.   {   
  18.     JFrame.setDefaultLookAndFeelDecorated(true); 
  19.     JFrame frame = new JFrame("Digital Clock");
  20.     frame.setSize(300,150); 
  21.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.     frame.setLayout(new GridLayout(3, 1));
  23.  
  24.     ClockLabel dateL = new ClockLabel("date"); 
  25.     ClockLabel timeL = new ClockLabel("time"); 
  26.     ClockLabel dayL = new ClockLabel("day"); 
  27.     
  28.     frame.add(dateL);
  29.     frame.add(timeL);
  30.     frame.add(dayL);
  31.  
  32.     frame.getContentPane().setBackground(Color.decode("#005B5C"));
  33.  
  34.     frame.setVisible(true);
  35.   }
  36.  }
  37.  
  38.  @SuppressWarnings("serial")

  39.  class ClockLabel extends JLabel implements ActionListener 
  40.  { 
  41.     String clockType;
  42.     SimpleDateFormat format;
  43.  
  44.     public ClockLabel(String clockType) {
  45.     this.clockType = clockType;
  46.     setForeground(Color.white);
  47.  
  48.     if(clockType.equals("time"))
  49.        {
  50.           format = new SimpleDateFormat("  MMMM dd yyyy");
  51.           setFont(new Font("monopaced", Font.ITALIC, 12));
  52.           setHorizontalAlignment(SwingConstants.LEFT);
  53.        }
  54.     else if(clockType.equals("time"))
  55.        {   
  56.           format = new SimpleDateFormat("hh:mm:ss a");
  57.           setFont(new Font("monospaced", Font.ITALIC, 40));
  58.           setHorizontalAlignment(SwingConstants.CENTER);
  59.        }
  60.     else if(clockType.equals("day"))  
  61.        {   
  62.           format = new SimpleDateFormat("EEEE ");
  63.           setFont(new Font("monospaced", Font.ITALIC, 12));
  64.           setHorizontalAlignment(SwingConstants.RIGHT);
  65.        }   
  66.     else
  67.        {   
  68.           format = new SimpleDateFormat();
  69.        }
  70.     }
  71.  
  72.  Timer timer = new Timer(1000, this);
  73.  timer.start();
  74.     }
  75.  
  76.     public void actionPerformed(ActionEvent event) {
  77.     Date date = new Date();
  78.     setText(format.format(date));
  79.  }
  80. }


RESULT


( If you like the content here, follow TSP on social media to get notified on future updates)



Comments

Share on Social Media

Most Viewed Posts

DS & A (Algorithm Analysis - Best, Worst and Average Case)

SPACE & DRAGONS

DS & A(Queue)