-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounter_Reset_OBJ.java
More file actions
60 lines (47 loc) · 1.45 KB
/
Counter_Reset_OBJ.java
File metadata and controls
60 lines (47 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
56
57
58
59
60
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Counter_Reset_OBJ extends JFrame {
/*
why here ? it's because, if we write these inside the constructor, it won't be
accessible out of the constructor.
*/
JButton b1, b2;
JTextField textField;
//constructor
Counter_Reset_OBJ() {
super("Counter Box");
setSize(400,100);
setLayout(new FlowLayout());
add(new JLabel("Counter : "));
textField = new JTextField(10);
textField.setText("0");
b1 = new JButton("add");
b2 = new JButton("reset");
add(textField); add(b1); add(b2);
b1.addActionListener(new Inner());
b2.addActionListener(new Inner());
setVisible(true);
}
class Inner implements ActionListener {
@Override
public void actionPerformed(ActionEvent actionEvent)
{
if(actionEvent.getSource() == b1) {
String extract = textField.getText();
int number = Integer.parseInt(extract);
number = number + 1;
extract = "" + number;
textField.setText(extract);
}
else if(actionEvent.getSource() == b2) {
textField.setText("0");
}
}
}
public static void main(String[] args) //main method
{
new Counter_Reset_OBJ();
}
}