Skip to content

Commit c15d7e5

Browse files
Merge pull request #456 from ArmanBhatia0100/digitalClock/ui-fix
Digital clock/UI fix
2 parents 98205b9 + 72466cd commit c15d7e5

File tree

3 files changed

+99
-34
lines changed

3 files changed

+99
-34
lines changed

Digital Clock/Clock.java

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

Digital Clock/DigitalClock.java

+25-15
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
//Digital Clock mini project
22

3-
4-
5-
import javax.swing.*;
63
import java.awt.*;
74
import java.text.SimpleDateFormat;
85
import java.util.Calendar;
6+
import javax.swing.*;
7+
import javax.swing.border.EmptyBorder;
98

109
public 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+
}

Software Piracy Control/nbproject/private/profiler/ResetResultsProfilingPoint.pp

-19
This file was deleted.

0 commit comments

Comments
 (0)