Repository navigation
Expand file tree
/
Copy pathSimpleCalculator.java
More file actions
116 lines (86 loc) · 3.16 KB
/
Copy pathSimpleCalculator.java
File metadata and controls
116 lines (86 loc) · 3.16 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
package simple_calculator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleCalculator {
public static void main(String[] args) {
CalculatorFrame frame = new CalculatorFrame();
frame.addWindowListener((new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}));
}
}
class CalculatorFrame extends JFrame implements ActionListener {
private JTextField firstValue;
private JTextField secondValue;
private JTextField resultValue;
public CalculatorFrame() {
JPanel pane = (JPanel) getContentPane();
pane.setLayout(new BorderLayout());
firstValue = new JTextField(10);
secondValue = new JTextField(10);
resultValue = new JTextField(10);
resultValue.setEditable(false);
JPanel inFieldPane = new JPanel();
inFieldPane.setLayout(new GridLayout(3, 2));
inFieldPane.add(new JLabel("First Value"));
inFieldPane.add(firstValue);
firstValue.addActionListener(this);
inFieldPane.add(new JLabel("Second Value"));
inFieldPane.add(secondValue);
secondValue.addActionListener(this);
inFieldPane.add(new JLabel("Result"));
inFieldPane.add(resultValue);
pane.add(inFieldPane, BorderLayout.NORTH);
// Add Clear Button
JPanel clearField = new JPanel();
clearField.setLayout(new FlowLayout());
JButton clearButton = new JButton("Clear");
clearField.add(clearButton);
clearButton.addActionListener(this);
pane.add(clearField, BorderLayout.CENTER);
// Add Operator Buttons
JPanel operatorField = new JPanel();
operatorField.setLayout(new FlowLayout());
// Create buttons
JButton addButton = new JButton("+");
addButton.addActionListener(this);
JButton subtractButton = new JButton("-");
subtractButton.addActionListener(this);
JButton multiplyButton = new JButton("*");
multiplyButton.addActionListener(this);
JButton divideButton = new JButton("/");
divideButton.addActionListener(this);
operatorField.add(addButton);
operatorField.add(subtractButton);
operatorField.add(multiplyButton);
operatorField.add(divideButton);
pane.add(operatorField, BorderLayout.SOUTH);
pack();
setSize(330, 172);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Clear")) {
firstValue.setText("");
secondValue.setText("");
resultValue.setText("");
}
double result = 0;
if (e.getActionCommand().equals("+")) {
result = Double.parseDouble(firstValue.getText().trim()) + Double.parseDouble(secondValue.getText().trim());
resultValue.setText(String.valueOf(result));
} else if (e.getActionCommand().equals("-")) {
result = Double.parseDouble(firstValue.getText().trim()) - Double.parseDouble(secondValue.getText().trim());
resultValue.setText(String.valueOf(result));
} else if (e.getActionCommand().equals("*")) {
result = Double.parseDouble(firstValue.getText().trim()) * Double.parseDouble(secondValue.getText().trim());
resultValue.setText(String.valueOf(result));
} else if (e.getActionCommand().equals("/")) {
result = Double.parseDouble(firstValue.getText().trim()) / Double.parseDouble(secondValue.getText().trim());
resultValue.setText(String.valueOf(result));
}
}
}