-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindow.java
138 lines (119 loc) · 3.59 KB
/
Window.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
import java.util.Date;
import java.text.*;
public class Window extends Frame{ //can extend JFrame
// private Label clockLabel, heading; //these can be JLabel
// private Font font;
private Label clock_heading, clock_time;
private Label date_heading, date;
private Label timeZone_heading, timeZone;
private Label spacer1, spacer2, spacer3, spacer4, last_spacer1, last_spacer2, last_spacer3, last_spacer4;
Window(String s){
setTitle(s); //could call this super as well
setSize(700, 400);
setLocation(300, 200);
setBackground(Color.MAGENTA);
setForeground(Color.black);
Font fn = new Font("MONOSPACED", Font.ROMAN_BASELINE, 32);
Font fn2 = new Font("Forte", Font.BOLD, 32);
setFont(fn);
// font = fn;
// this.createGUI();
clock_heading = new Label("Time: ");
clock_time = new Label("Clock");
date_heading = new Label("Date: ");
date = new Label("Date");
timeZone_heading = new Label("TimeZone: ");
timeZone = new Label("TimeZone");
spacer1 = new Label();
spacer2 = new Label();
spacer3 = new Label();
spacer4 = new Label();
last_spacer1 = new Label();
last_spacer2 = new Label();
// last_spacer3 = new Label();
// last_spacer4 = new Label();
clock_time.setFont(fn2);
clock_heading.setFont(fn);
date_heading.setFont(fn);
date.setFont(fn2);
timeZone_heading.setFont(fn);
timeZone.setFont(fn2);
add(spacer1);
this.add(clock_heading);
this.add(clock_time);
add(spacer3);
this.add(spacer2);
add(date_heading);
add(date);
this.add(spacer4);
startClock();
add(last_spacer1);
add(timeZone_heading);
add(timeZone);
add(last_spacer2);
// add(last_spacer3);
// add(last_spacer4);
// setLayout(new FlowLayout());
setLayout(new GridLayout(3, 4));
setVisible(true);
setResizable(false);
// isResizable();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
/*public void createGUI(){
clockLabel = new JLabel("Clock");
heading = new JLabel("My Clock");
clockLabel.setFont(font);
heading.setFont(font);
super.setBackground(Color.BLACK);
super.setForeground(Color.RED);
this.setLayout(new GridLayout(2, 1));
this.add(heading);
this.add(clockLabel);
this.startClock();
}*/
public void startClock(){
/*Timer timer = new Timer(1000, new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
// String dateTime = new Date().toString();
// String dateTime = new Date().toLocaleString();
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("hh : mm : ss a");
String dateTime = sdf.format(d);
clock_time.setText(dateTime);
// @SuppressWarnings
// date.setText(new Date().toLocaleString());
date.setText(d.toString());
}
}); //(millisecond, actionlistener)
timer.start();*/ //Thread is doing the same as of this timer will perform
Thread t = new Thread(){
@Override
public void run(){
try{
while(true){
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("hh : mm : ss a");
String dateTime = sdf.format(d);
clock_time.setText(dateTime);
date.setText(d.toString());
timeZone.setText(" "+d.toString().substring(20, d.toString().length()));
Thread.currentThread().sleep(1000); //Thread.sleep(1000);
}
} catch (InterruptedException e){
e.printStackTrace();
}
}
};
t.start();
}
}