-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clock.java
115 lines (88 loc) · 2.62 KB
/
Clock.java
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
115
package map;
import java.awt.FlowLayout;
import java.awt.Font;
import java.text.DateFormat;
import java.util.Date;
import java.util.Timer;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.util.TimerTask;
public class Clock {
private JLabel clockLabel;
private DateFormat formatter = DateFormat.getDateTimeInstance();
public Clock() {
JFrame frame = initFrame();
clockLabel = initClockLabel();
frame.getContentPane().add(clockLabel);
frame.pack();
frame.setVisible(true);
initTimer();
}
public Clock(String imagefile) {
JFrame frame = initFrame();
clockLabel = initClockLabel();
JLabel background = initBackground(imagefile, clockLabel);
frame.getContentPane().add(background);
frame.pack();
frame.setVisible(true);
initTimer();
}
private JFrame initFrame() {
JFrame frame = new JFrame("Java Clock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return frame;
}
private JLabel initClockLabel() {
JLabel label = new JLabel();
Font font = label.getFont();
font = new Font(font.getFontName(), font.getStyle(), font.getSize() + 4);
label.setFont(font);
label.setText(formatter.format(new Date(System.currentTimeMillis())));
return label;
}
private JLabel initBackground(String imagefile, JLabel label) {
ImageIcon icon = new ImageIcon(imagefile);
JLabel background = new JLabel(icon);
background.setLayout(new FlowLayout());
background.add(label);
return background;
}
private void initTimer() {
Timer timer = new Timer();
// 2 秒後からスタート
Date start = new Date((System.currentTimeMillis() / 1000L) * 1000L + 2000L);
timer.scheduleAtFixedRate(new ClockTask(this), start, 1000L);
}
public void update(long time) {
clockLabel.setText(formatter.format(new Date(time)));
}
/*
public static void main(String[] args) {
if (args.length > 0) {
new Clock(args[0]);
} else {
new Clock();
}
}
*/
}
class ClockTask extends TimerTask {
private Clock clock;
public ClockTask(Clock clock) {
this.clock = clock;
}
public void run() {
clock.update(scheduledExecutionTime());
}
}
class splp extends Thread{
public splp(){
try {
System.out.println("SLEEP 1");
sleep(2000);
System.out.println("SLEEP 2");
} catch (InterruptedException e) {
}
}
}