-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditableCounter.java
More file actions
61 lines (52 loc) · 2.11 KB
/
EditableCounter.java
File metadata and controls
61 lines (52 loc) · 2.11 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class EditableCounter {
JFrame frame = new JFrame("Counter");
JButton count = new JButton("count");
JButton reset = new JButton("reset");
JTextField textField;
// this constructor takes the inputted value from main method
EditableCounter() {
frame.setSize(400,150);
frame.setLayout(new FlowLayout()); //otherwise, multiple components wouldn't be visible
// test field will have used user inputted value
textField = new JTextField("0", 10);
//adding components to the JFrame
frame.add(count);
frame.add(textField);
frame.add(reset);
// to perform action using buttons
count.addActionListener(new Inner()); // pressing the count button will increase the text field value by 1
reset.addActionListener(new Inner()); // pressing the reset button will set the text field to '0'
frame.setVisible(true); // only way to make the frame visible
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class Inner implements ActionListener {
@Override
public void actionPerformed(ActionEvent actionEvent)
{
//detecting which button is pressed via getting source
if(actionEvent.getSource() == count)
{
// try-catch : don't wanna get any exception if the user enters random characters
try {
int num = Integer.parseInt(textField.getText());
textField.setText((num + 1) + ""); // the value in text field + 1
}
catch (NumberFormatException e){
System.out.println("you should've gone for a number in the text field");
}
}
// warning : pressing the reset button will set the text field to '0'
else {
textField.setText("0");
}
}
}
// main method
public static void main(String[] args) {
new EditableCounter();
}
}