0% found this document useful (0 votes)
953 views

Java Project 2. Simple Clock PDF

This document contains the code for a Java class called DateTime that displays the current date and time in a graphical user interface (GUI) frame. It uses threads to continuously update the displayed time every second. The main method launches the application and initializes the frame. The initialize method sets up the frame layout and label. The datetime method starts a thread that runs forever, getting the current date and time and updating the label.

Uploaded by

Krish Stark
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
953 views

Java Project 2. Simple Clock PDF

This document contains the code for a Java class called DateTime that displays the current date and time in a graphical user interface (GUI) frame. It uses threads to continuously update the displayed time every second. The main method launches the application and initializes the frame. The initialize method sets up the frame layout and label. The datetime method starts a thread that runs forever, getting the current date and time and updating the label.

Uploaded by

Krish Stark
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

DateTime.

java

1 package datetime;
2
3 import java.awt.EventQueue;
10
11 public class DateTime {
12
13 private JFrame frame;
14 private JLabel lblClock;
15
16 /**
17 * Launch the application.
18 */
19 public static void main(String[] args) {
20 EventQueue.invokeLater(new Runnable() {
21 public void run() {
22 try {
23 DateTime window = new DateTime();
24 window.frame.setVisible(true);
25 } catch (Exception e) {
26 e.printStackTrace();
27 }
28 }
29 });
30 }
31
32 public void datetime()
33 {
34 Thread datetime = new Thread()
35 {
36 public void run()
37 {
38 try
39 {
40 for(;;)
41 {
42 Calendar cal = new GregorianCalendar();
43 int day = cal.get(Calendar.DAY_OF_MONTH);
44 int month = cal.get(Calendar.MONTH);
45 int year = cal.get(Calendar.YEAR);
46
47 int second = cal.get(Calendar.SECOND);
48 int minute = cal.get(Calendar.MINUTE);
49 int hour = cal.get(Calendar.HOUR);
50
51 lblClock.setText("Time " + hour + ":" + minute + ":" + second
+ " " + "Date " + year + "/" + month + "/" + day);
52
53 sleep(1000);
54 }
55 }
56 catch(Exception e)
57 {
58
59 }
60 }
61 };
62 datetime.start();

Page 1
DateTime.java

63 }
64
65 /**
66 * Create the application.
67 */
68 public DateTime() {
69 initialize();
70 datetime();
71 }
72
73 /**
74 * Initialize the contents of the frame.
75 */
76 private void initialize() {
77 frame = new JFrame();
78 frame.setBounds(100, 100, 703, 365);
79 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
80 frame.getContentPane().setLayout(null);
81
82 lblClock = new JLabel("Clock");
83 lblClock.setFont(new Font("Tahoma", Font.BOLD, 25));
84 lblClock.setBounds(24, 69, 640, 175);
85 frame.getContentPane().add(lblClock);
86 }
87
88 }
89

Page 2

You might also like