-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock.java
More file actions
28 lines (23 loc) · 919 Bytes
/
Clock.java
File metadata and controls
28 lines (23 loc) · 919 Bytes
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
import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Clock {
private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");
public static void main(String[] args) {
JFrame frame = new JFrame("Clock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 100);
frame.setLayout(new BorderLayout());
JLabel timeLabel = new JLabel();
timeLabel.setHorizontalAlignment(SwingConstants.CENTER);
timeLabel.setFont(new Font("Arial", Font.BOLD, 24));
frame.add(timeLabel, BorderLayout.CENTER);
Timer timer = new Timer(1000, e -> {
String currentTime = TIME_FORMAT.format(new Date());
timeLabel.setText(currentTime);
});
timer.start();
frame.setVisible(true);
}
}