-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_clock.java
55 lines (45 loc) · 1.6 KB
/
basic_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
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
public class basic_clock extends JFrame
{
JLabel date, time;
public basic_clock()
{
super("clock");
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Calendar current = Calendar.getInstance();
current.setTime(new Date());
date.setText((current.get(Calendar.MONTH) + 1) +"/" +current.get(Calendar.DATE) +"/" +current.get(Calendar.YEAR));
String timeStr = String.format("%d:%02d:%02d", current.get(Calendar.HOUR), current.get(Calendar.MINUTE), current.get(Calendar.SECOND));
time.setText(timeStr);
}
};
date = new JLabel();
time = new JLabel();
setLayout(new FlowLayout());
setSize(310,190);
setResizable(false);
setVisible(true);
add(date);
add(time);
date.setFont(new Font("Arial", Font.BOLD, 64));
time.setFont(new Font("Arial", Font.BOLD, 64));
javax.swing.Timer timer = new javax.swing.Timer(500, listener);
timer.setInitialDelay(0);
timer.start();
}
public static void main(String args[])
{
basic_clock c = new basic_clock();
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}