11//Digital Clock mini project
22
3-
4-
5- import javax .swing .*;
63import java .awt .*;
74import java .text .SimpleDateFormat ;
85import java .util .Calendar ;
6+ import javax .swing .*;
7+ import javax .swing .border .EmptyBorder ;
98
109public class Clock extends JFrame {
1110
@@ -20,33 +19,43 @@ public class Clock extends JFrame {
2019 String time ;
2120 String day ;
2221 String date ;
22+
2323 Clock () {
24+
2425 this .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
2526 this .setTitle ("Digital Clock" );
2627 this .setLayout (new FlowLayout ());
27- this . setSize ( 350 , 220 );
28+
2829 this .setResizable (false );
2930
3031 timeFormat = new SimpleDateFormat ("hh:mm:ss a" );
31- dayFormat = new SimpleDateFormat ("EEEE" );
32- dateFormat = new SimpleDateFormat ("dd MMMMM, yyyy" );
32+ dayFormat = new SimpleDateFormat ("EEEE" );
33+ dateFormat = new SimpleDateFormat ("dd MMMMM, yyyy" );
3334 timeLabel = new JLabel ();
3435 timeLabel .setFont (new Font ("SANS_SERIF" , Font .PLAIN , 59 ));
3536 timeLabel .setBackground (Color .BLACK );
3637 timeLabel .setForeground (Color .WHITE );
3738 timeLabel .setOpaque (true );
38- dayLabel = new JLabel ();
39- dayLabel .setFont (new Font ("Ink Free" ,Font .BOLD ,34 ));
39+ dayLabel = new JLabel ();
40+ dayLabel .setFont (new Font ("Ink Free" , Font .BOLD , 34 ));
4041
41- dateLabel = new JLabel ();
42- dateLabel .setFont (new Font ("Ink Free" ,Font .BOLD ,30 ));
42+ dateLabel = new JLabel ();
43+ dateLabel .setFont (new Font ("Ink Free" , Font .BOLD , 30 ));
4344
45+ //Creating JPanel to add all the content.
46+ // Used Box layout to align components on Y-axis.
47+ JPanel mainPanel = new JPanel ();
48+ mainPanel .setLayout (new BoxLayout (mainPanel , BoxLayout .Y_AXIS ));
49+ mainPanel .setBorder (BorderFactory .createEmptyBorder (10 , 10 , 10 , 10 ));
50+ mainPanel .setPreferredSize (new Dimension (400 , 170 ));
4451
45- this .add (timeLabel );
46- this .add (dayLabel );
47- this .add (dateLabel );
48- this .setVisible (true );
52+ mainPanel .add (timeLabel );
53+ mainPanel .add (dayLabel );
54+ mainPanel .add (dateLabel );
4955
56+ this .add (mainPanel );
57+ this .setVisible (true );
58+ this .pack ();
5059 setTimer ();
5160 }
5261
@@ -68,7 +77,8 @@ public void setTimer() {
6877 }
6978 }
7079 }
80+
7181 public static void main (String [] args ) {
7282 new Clock ();
7383 }
74- }
84+ }
0 commit comments