forked from sarthakd999/Hacktoberfest2021-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clock.java
67 lines (53 loc) · 1.33 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
// This is a clock program made with Java
package pratice;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
public class Clock extends JFrame
{
public static void main(String[] args) {
System.out.println("Testing......");
MyWindow w=new MyWindow();
}
private JLabel heading;
private JLabel clockLabel;
private Font font=new Font("Times New Roman",Font.BOLD,35);
MyWindow()
{
super.setTitle("My Clock");
super.setSize(400,400);
super.setLocation(300,50);
this.creatGUI();
this.startClock();
super.setVisible(true);
}
public void creatGUI()
{
heading=new JLabel("My Clock");
clockLabel=new JLabel("Clock");
heading.setFont(font);
clockLabel.setFont(font);
this.setLayout(new GridLayout(2,1));
this.add(heading);
this.add(clockLabel);
}
public void startClock()
{
Timer timer=new Timer(1000,new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// String dateTime=new Date().toString();
// String dateTime=new Date().toLocaleString();
Date d=new Date();
SimpleDateFormat sfd=new SimpleDateFormat("d/M/y hh:mm:ss a");
String dateTime=sfd.format(d);
clockLabel.setText(dateTime);
}
});
timer.start();
}
}