1
1
//Digital Clock mini project
2
2
3
-
4
-
5
- import javax .swing .*;
6
3
import java .awt .*;
7
4
import java .text .SimpleDateFormat ;
8
5
import java .util .Calendar ;
6
+ import javax .swing .*;
7
+ import javax .swing .border .EmptyBorder ;
9
8
10
9
public class Clock extends JFrame {
11
10
@@ -20,33 +19,43 @@ public class Clock extends JFrame {
20
19
String time ;
21
20
String day ;
22
21
String date ;
22
+
23
23
Clock () {
24
+
24
25
this .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
25
26
this .setTitle ("Digital Clock" );
26
27
this .setLayout (new FlowLayout ());
27
- this . setSize ( 350 , 220 );
28
+
28
29
this .setResizable (false );
29
30
30
31
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" );
33
34
timeLabel = new JLabel ();
34
35
timeLabel .setFont (new Font ("SANS_SERIF" , Font .PLAIN , 59 ));
35
36
timeLabel .setBackground (Color .BLACK );
36
37
timeLabel .setForeground (Color .WHITE );
37
38
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 ));
40
41
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 ));
43
44
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 ));
44
51
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 );
49
55
56
+ this .add (mainPanel );
57
+ this .setVisible (true );
58
+ this .pack ();
50
59
setTimer ();
51
60
}
52
61
@@ -68,7 +77,8 @@ public void setTimer() {
68
77
}
69
78
}
70
79
}
80
+
71
81
public static void main (String [] args ) {
72
82
new Clock ();
73
83
}
74
- }
84
+ }
0 commit comments