This repository has been archived by the owner on Apr 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculatorGui.java
124 lines (121 loc) · 4.55 KB
/
calculatorGui.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
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
117
118
119
120
121
122
123
124
import javax.swing.*;
public class calculatorGui {
double a, b, operationResult;
String operation;
private JPanel calculatorArea;
private JTextField result;
private JButton Two;
private JButton Clear;
private JButton Square;
private JButton Eight;
private JButton Five;
private JButton Equal;
private JButton Nine;
private JButton Six;
private JButton Three;
private JButton Zero;
private JButton Addition;
private JButton Exponent;
private JButton Seven;
private JButton Four;
private JButton One;
private JButton Modulo;
private JButton Sottration;
private JButton Moltiplication;
private JButton Division;
private JButton Comma;
public calculatorGui() {
Clear.addActionListener(e -> result.setText(""));
Zero.addActionListener(e -> result.setText(result.getText() + Zero.getText()));
One.addActionListener(e -> result.setText(result.getText() + One.getText()));
Two.addActionListener(e -> result.setText(result.getText() + Two.getText()));
Three.addActionListener(e -> result.setText(result.getText() + Three.getText()));
Four.addActionListener(e -> result.setText(result.getText() + Four.getText()));
Five.addActionListener(e -> result.setText(result.getText() + Five.getText()));
Six.addActionListener(e -> result.setText(result.getText() + Six.getText()));
Seven.addActionListener(e -> result.setText(result.getText() + Seven.getText()));
Eight.addActionListener(e -> result.setText(result.getText() + Eight.getText()));
Nine.addActionListener(e -> result.setText(result.getText() + Nine.getText()));
Addition.addActionListener(e -> {
a = Double.parseDouble(result.getText());
operation = "+";
result.setText("");
});
Sottration.addActionListener(e -> {
a = Double.parseDouble(result.getText());
operation = "-";
result.setText("");
});
Moltiplication.addActionListener(e -> {
a = Double.parseDouble(result.getText());
operation = "*";
result.setText("");
});
Division.addActionListener(e -> {
a = Double.parseDouble(result.getText());
operation = "/";
result.setText("");
});
Modulo.addActionListener(e -> {
a = Double.parseDouble(result.getText());
operation = "%";
result.setText("");
});
Exponent.addActionListener(e -> {
a = Double.parseDouble(result.getText());
operation = "^";
result.setText("");
});
Square.addActionListener(e -> {
a = Double.parseDouble(result.getText());
operation = "v";
result.setText("");
});
Comma.addActionListener(e -> {
result.getText();
{
result.setText(result.getText() + Comma.getText());
}
});
Equal.addActionListener(e -> {
b = Double.parseDouble(result.getText());
switch (operation) {
case "+" -> {
operationResult = a + b;
result.setText(String.valueOf(operationResult));
}
case "-" -> {
operationResult = a - b;
result.setText(String.valueOf(operationResult));
}
case "*" -> {
operationResult = a * b;
result.setText(String.valueOf(operationResult));
}
case "/" -> {
operationResult = a / b;
result.setText(String.valueOf(operationResult));
}
case "%" -> {
operationResult = a % b;
result.setText(String.valueOf(operationResult));
}
case "^" -> {
operationResult = Math.pow(a, b);
result.setText(String.valueOf(operationResult));
}
case "v" -> {
operationResult = Math.sqrt(a);
result.setText(String.valueOf(operationResult));
}
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("calculatorGui");
frame.setContentPane(new calculatorGui().calculatorArea);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}