-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounter_Reset.java
More file actions
81 lines (62 loc) · 2.13 KB
/
Counter_Reset.java
File metadata and controls
81 lines (62 loc) · 2.13 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Counter_Reset {
JFrame frame;
JTextField textField;
Counter_Reset() {
frame = new JFrame("Frame");
frame.setSize(400, 150);
frame.setLayout(new FlowLayout());
JButton addButton = new JButton("add");
JButton resetButton = new JButton("reset");
textField = new JTextField();
textField.setText("0");
textField.setColumns(10);
frame.add(new JLabel("counter"));
frame.add(textField);
frame.add(addButton);
frame.add(resetButton);
// Event source : button
// Event Listener : new inner (); // Action Performed
addButton.addActionListener(new Inner()); //register event listener for event source
resetButton.addActionListener(new Inner2()); //register event listener for event source
frame.setVisible(true);
}
class Inner implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// step 1 : extract the current value of the counter
String s = textField.getText();
// step 2 : add 1 to the current value
int number = Integer.parseInt(s);
number = number + 1;
s = "" + number;
// step 3 : set this new value to the counter
textField.setText(s);
}
}
class Inner2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
/* either :
// step 1 : extract the current value of the counter
String s = textField.getText();
// step 2 : add 1 to the current value
int number = Integer.parseInt(s);
number = 0;
s = "" + number;
// step 3 : set this new value to the counter
textField.setText(s);
*/
//or :
textField.setText("0");
}
}
//main method
public static void main(String[] args)
{
new Counter_Reset();
}
}