-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculatorApp.java
79 lines (69 loc) · 2.45 KB
/
CalculatorApp.java
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CalculatorApp {
private JFrame frame;
private JTextField textField;
private double num1 = 0, num2 = 0;
private String operator = "";
public CalculatorApp() {
frame = new JFrame("Calculator");
frame.setLayout(new BorderLayout());
textField = new JTextField();
textField.setEditable(false);
frame.add(textField, BorderLayout.NORTH);
JPanel panel = new JPanel(new GridLayout(4, 4));
frame.add(panel, BorderLayout.CENTER);
String[] buttons = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+"
};
for (String button : buttons) {
JButton btn = new JButton(button);
btn.addActionListener(new ButtonClickListener());
panel.add(btn);
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
private class ButtonClickListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (Character.isDigit(command.charAt(0)) || command.equals(".")) {
textField.setText(textField.getText() + command);
} else if (command.equals("C")) {
textField.setText("");
} else if (command.equals("=")) {
num2 = Double.parseDouble(textField.getText());
double result = 0;
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
}
textField.setText(String.valueOf(result));
num1 = result;
} else {
operator = command;
num1 = Double.parseDouble(textField.getText());
textField.setText("");
}
}
}
public static void main(String[] args) {
new CalculatorApp();
}
}