-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyClock.java
More file actions
114 lines (96 loc) · 3.15 KB
/
MyClock.java
File metadata and controls
114 lines (96 loc) · 3.15 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* * * * * * * * * * *\
* MyClock.java
* Description: Clock to show time, has two construstors for different clock styles, constructor #1 is meant for a small and
* simple clock, it is used in DefaultGui and AdminGUI in the button panels. Constructor #2 is meant to be a more
* advanced clock and shows time, day of week, and the date, used in AdminGui's homepage, each clock type is a JPanel
* and can be added to the program as a component.
*
* Date: 5/7/16
* @author Brandon Ballard
\* * * * * * * * * * */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
import javax.swing.Timer;
//Simple clock shows time and AM/PM
class MyClock extends JPanel implements ActionListener
{
JLabel timeLabel, dayLabel, dateLabel, clockLabel;
SimpleDateFormat timeFormat, dayFormat, dateFormat, df;
Timer timer;
boolean advanced;
public MyClock()
{
advanced = false;
df = new SimpleDateFormat("h:mm:ss a");
clockLabel = new JLabel();
clockLabel.setForeground(Color.GREEN);
clockLabel.setFont(new Font("Courier", Font.BOLD,14));
clockLabel.setText(df.format(new Date()));
timer = new Timer(500, this);
timer.setRepeats(true);
timer.start();
setUpGUI();
//Add components
add(clockLabel, BorderLayout.CENTER);
}
//More advanced clock, shows time, day of week, and date
public MyClock(String s)
{
advanced = true;
timeFormat = new SimpleDateFormat("h:mm:ss a");
dayFormat = new SimpleDateFormat("EEEE");
dateFormat = new SimpleDateFormat("M/d/yyyy");
dateLabel = new JLabel();
dateLabel.setForeground(Color.GRAY);
dateLabel.setFont(new Font("Arial", Font.PLAIN,15));
dateLabel.setText(dateFormat.format(new Date()) + " ");
dayLabel = new JLabel();
dayLabel.setForeground(Color.GRAY);
dayLabel.setFont(new Font("Arial", Font.PLAIN,15));
dayLabel.setText(" " + dayFormat.format(new Date()));
timeLabel = new JLabel();
timeLabel.setForeground(Color.GRAY);
timeLabel.setFont(new Font("Arial", Font.PLAIN,50));
timeLabel.setText(timeFormat.format(new Date()));
timer = new Timer(500, this);
timer.setRepeats(true);
timer.start();
setUpGUI();
//Add components
add(dateLabel, BorderLayout.NORTH);
add(timeLabel, BorderLayout.CENTER);
add(dayLabel, BorderLayout.SOUTH);
}
//This is where the clocks get updated, the timer will call this method
public void actionPerformed(ActionEvent ae)
{
if(advanced)
{
timeLabel.setText(timeFormat.format(new Date()));
dateLabel.setText(dateFormat.format(new Date()) + " ");
dayLabel.setText(" " + dayFormat.format(new Date()));
}
else
{
clockLabel.setText(df.format(new Date()));
}
}
public void setUpGUI()
{
if(advanced)
{
setBackground((Color.WHITE));
setMaximumSize(new Dimension(300, 120));
setMinimumSize(new Dimension(300, 120));
setPreferredSize(new Dimension(300, 120));
}
else
{
setBackground((Color.BLACK));
setMaximumSize(new Dimension(100, 30));
}
}
}