-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.java
More file actions
64 lines (56 loc) · 1.4 KB
/
timer.java
File metadata and controls
64 lines (56 loc) · 1.4 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
package mayank;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.tools.Tool;
public class timer extends JFrame {
private JButton bt;
private JTextField tf;
private JLabel first,second;
private Timer timer;
int counter;
timer(){
super("the title");
setLayout(new GridLayout(2,2));
first=new JLabel("Enter Seconds:");
add(first,SwingConstants.CENTER);
tf=new JTextField(5);
add(tf);
bt=new JButton("PRESS TO START");
add(bt);
second=new JLabel("Waiting..");
add(second);
//1000 is in milliseconds
timer=new Timer(1000, null);
bt.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e) {
counter=(int)(Integer.parseInt(tf.getText()));
second.setText("Time Left:"+counter);
timer.start();
timer.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e) {
counter--;
if(counter>=1)
second.setText("Time Left:"+counter);
else{
getToolkit().beep();
timer.stop();
second.setText("Times Up!!");
}
}
}
);
}
}
);
}
public static void main(String[] args) {
timer ob=new timer();
ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ob.setSize(300,100);
ob.setVisible(true);
}
}