-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigitalClock.java
More file actions
55 lines (42 loc) · 1.45 KB
/
Copy pathDigitalClock.java
File metadata and controls
55 lines (42 loc) · 1.45 KB
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
package digitalClock;
import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DigitalClock extends JFrame {
private JLabel clockLabel;
public DigitalClock() {
setTitle("Digital Clock");
setSize(200, 100);
setUndecorated(true);
setAlwaysOnTop(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ClockPanel panel = new ClockPanel();
panel.setLayout(new BorderLayout());
clockLabel = new JLabel("", JLabel.CENTER);
clockLabel.setFont(new Font("Arial", Font.BOLD, 28));
clockLabel.setForeground(Color.YELLOW);
panel.add(clockLabel, BorderLayout.CENTER);
add(panel);
new Timer(1000, e -> updateClock()).start();
updateClock();
setLocationRelativeTo(null);
setVisible(true);
}
private void updateClock() {
clockLabel.setText(new SimpleDateFormat("hh:mm:ss a").format(new Date()));
}
class ClockPanel extends JPanel {
private final Image backgroundImage;
ClockPanel() {
backgroundImage = new ImageIcon("media/background.png").getImage();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(DigitalClock::new);
}
}