-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyFrame.java
More file actions
83 lines (64 loc) · 2.23 KB
/
MyFrame.java
File metadata and controls
83 lines (64 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/*
* Harrison Holsinger
* This program displays the current day, date, and time
*/
public class MyFrame extends JFrame {
SimpleDateFormat dayFormat;
SimpleDateFormat dateFormat;
SimpleDateFormat timeFormat;
JLabel dayLabel;
JLabel dateLabel;
JLabel timeLabel;
String day;
String date;
String time;
MyFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("My Clock Program");
this.setLayout(new FlowLayout());
this.setSize(600, 200);
this.getContentPane().setBackground(Color.BLACK);
dayFormat = new SimpleDateFormat("EEEE, ");
dateFormat = new SimpleDateFormat("MMMM d, YYYY");
timeFormat = new SimpleDateFormat("hh:mm:ss a");
dayLabel = new JLabel();
dayLabel.setFont(new Font("Times New Roman", Font.PLAIN, 50));
dayLabel.setForeground(Color.WHITE);
dateLabel = new JLabel();
dateLabel.setFont(new Font("Times New Roman", Font.PLAIN, 50));
dateLabel.setForeground(Color.WHITE);
timeLabel = new JLabel();
timeLabel.setFont(new Font("Times New Roman", Font.PLAIN, 50));
timeLabel.setForeground(Color.WHITE);
this.add(dayLabel);
this.add(dateLabel);
this.add(timeLabel);
this.setLocationRelativeTo(null);
this.setVisible(true);
setTime();
}
/*
* This method gets the current day, date, and time
* It utilizes a while loop to constantly update the current time
* The sleep function pauses the loop so the method updates roughly every second
*/
public void setTime() {
while(true) {
day = dayFormat.format(Calendar.getInstance().getTime());
dayLabel.setText(day);
date = dateFormat.format(Calendar.getInstance().getTime());
dateLabel.setText(date);
time = timeFormat.format(Calendar.getInstance().getTime());
timeLabel.setText(time);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}