forked from cat2k22/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountdownTimer
More file actions
126 lines (107 loc) · 2.65 KB
/
CountdownTimer
File metadata and controls
126 lines (107 loc) · 2.65 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class Main {
JFrame window;
JLabel counterLabel;
Font font1 = new Font("Arial", Font.PLAIN, 70);
Timer timer;
int second, minute;
String ddSecond, ddMinute;
DecimalFormat dFormat = new DecimalFormat("00");
public static void main(String[] args) {
new Main();
}
public Main() {
window = new JFrame();
window.setSize(800,600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(null);
counterLabel = new JLabel("");
counterLabel.setBounds(300, 230, 200, 100);
counterLabel.setHorizontalAlignment(JLabel.CENTER);
counterLabel.setFont(font1);
window.add(counterLabel);
window.setVisible(true);
// Enable this to use Simple Timer
// second = 0;
// simpleTimer();
// timer.start();
// Enable this to use Normal Timer
// counterLabel.setText("00:00");
// second =0;
// minute =0;
// normalTimer();
// timer.start();
// Countdown Timer
counterLabel.setText("03:00");
second =0;
minute =3;
countdownTimer();
timer.start();
}
// Enable this to use Simple Timer
// public void simpleTimer() {
//
// timer = new Timer(1000, new ActionListener() {
//
// @Override
// public void actionPerformed(ActionEvent e) {
//
// second++;
//
// counterLabel.setText(""+ second);
// }
// });
// }
// Enable this to use Normal Timer
// public void normalTimer() {
//
// timer = new Timer(1000, new ActionListener() {
//
// @Override
// public void actionPerformed(ActionEvent e) {
//
// second++;
//
// ddSecond = dFormat.format(second);
// ddMinute = dFormat.format(minute);
// counterLabel.setText(ddMinute + ":" + ddSecond);
//
// if(second==60) {
// second=0;
// minute++;
//
// ddSecond = dFormat.format(second);
// ddMinute = dFormat.format(minute);
// counterLabel.setText(ddMinute + ":" + ddSecond);
// }
// }
// });
// }
public void countdownTimer() {
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
second--;
ddSecond = dFormat.format(second);
ddMinute = dFormat.format(minute);
counterLabel.setText(ddMinute + ":" + ddSecond);
if(second==-1) {
second = 59;
minute--;
ddSecond = dFormat.format(second);
ddMinute = dFormat.format(minute);
counterLabel.setText(ddMinute + ":" + ddSecond);
}
if(minute==0 && second==0) {
timer.stop();
}
}
});
}
}