-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7. DigitalClock.java
More file actions
30 lines (25 loc) · 919 Bytes
/
7. DigitalClock.java
File metadata and controls
30 lines (25 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
29
30
import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DigitalClock {
public static void main(String[] args) {
JFrame frame = new JFrame("Digital Clock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.getContentPane().setBackground(Color.GREEN);
frame.setLayout(new FlowLayout());
JLabel timeLabel = new JLabel();
timeLabel.setFont(new Font("Arial Black", Font.PLAIN, 24));
timeLabel.setForeground(Color.red);
frame.add(timeLabel);
frame.setVisible(true);
Timer timer = new Timer(1000, e -> {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date now = new Date();
String timeStr = sdf.format(now);
timeLabel.setText(timeStr);
});
timer.start();
}
}