Skip to content

Commit 72466cd

Browse files
updated the UI and added some padding
1 parent 24c7a44 commit 72466cd

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

Digital Clock/DigitalClock.java

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//Digital Clock mini project
2+
3+
import java.awt.*;
4+
import java.text.SimpleDateFormat;
5+
import java.util.Calendar;
6+
import javax.swing.*;
7+
import javax.swing.border.EmptyBorder;
8+
9+
public class Clock extends JFrame {
10+
11+
Calendar calendar;
12+
SimpleDateFormat timeFormat;
13+
SimpleDateFormat dayFormat;
14+
SimpleDateFormat dateFormat;
15+
16+
JLabel timeLabel;
17+
JLabel dayLabel;
18+
JLabel dateLabel;
19+
String time;
20+
String day;
21+
String date;
22+
23+
Clock() {
24+
25+
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
26+
this.setTitle("Digital Clock");
27+
this.setLayout(new FlowLayout());
28+
29+
this.setResizable(false);
30+
31+
timeFormat = new SimpleDateFormat("hh:mm:ss a");
32+
dayFormat = new SimpleDateFormat("EEEE");
33+
dateFormat = new SimpleDateFormat("dd MMMMM, yyyy");
34+
timeLabel = new JLabel();
35+
timeLabel.setFont(new Font("SANS_SERIF", Font.PLAIN, 59));
36+
timeLabel.setBackground(Color.BLACK);
37+
timeLabel.setForeground(Color.WHITE);
38+
timeLabel.setOpaque(true);
39+
dayLabel = new JLabel();
40+
dayLabel.setFont(new Font("Ink Free", Font.BOLD, 34));
41+
42+
dateLabel = new JLabel();
43+
dateLabel.setFont(new Font("Ink Free", Font.BOLD, 30));
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));
51+
52+
mainPanel.add(timeLabel);
53+
mainPanel.add(dayLabel);
54+
mainPanel.add(dateLabel);
55+
56+
this.add(mainPanel);
57+
this.setVisible(true);
58+
this.pack();
59+
setTimer();
60+
}
61+
62+
public void setTimer() {
63+
while (true) {
64+
time = timeFormat.format(Calendar.getInstance().getTime());
65+
timeLabel.setText(time);
66+
67+
day = dayFormat.format(Calendar.getInstance().getTime());
68+
dayLabel.setText(day);
69+
70+
date = dateFormat.format(Calendar.getInstance().getTime());
71+
dateLabel.setText(date);
72+
73+
try {
74+
Thread.sleep(1000);
75+
} catch (Exception e) {
76+
e.getStackTrace();
77+
}
78+
}
79+
}
80+
81+
public static void main(String[] args) {
82+
new Clock();
83+
}
84+
}

0 commit comments

Comments
 (0)