-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculatorFX_U1910060.java
189 lines (180 loc) Β· 6.33 KB
/
CalculatorFX_U1910060.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
Student:
ID: U1910060
Alimov Abdullokh
MSC2070-002
*/
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
public class CalculatorFX_U1910060 extends Application {
String value;
String math;
double num1, num2, result;
@Override
public void start(Stage primaryStage) throws Exception {
// Text Field
TextField jtfSpace=new TextField();
// Button Field
Button bOne = new Button (" 1 ");
Button bTwo = new Button (" 2 ");
Button bThree = new Button (" 3 ");
Button bFour = new Button (" 4 ");
Button bFive = new Button (" 5 ");
Button bSix = new Button (" 6 ");
Button bSeven = new Button (" 7 ");
Button bEight = new Button (" 8 ");
Button bNine = new Button (" 9 ");
Button bZero = new Button (" 0 ");
Button bAdd = new Button (" + ");
Button bSub = new Button (" - ");
Button bMul = new Button (" * ");
Button bDiv = new Button (" / ");
Button bEqual = new Button (" = ");
Button bDot = new Button (" . ");
bOne.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
jtfSpace.setText(jtfSpace.getText() + "1");
}
});
bTwo.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
jtfSpace.setText(jtfSpace.getText() + "2");
}
});
bThree.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
jtfSpace.setText(jtfSpace.getText() + "3");
}
});
bFour.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
jtfSpace.setText(jtfSpace.getText() + "4");
}
});
bFive.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
jtfSpace.setText(jtfSpace.getText() + "5");
}
});
bSix.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
jtfSpace.setText(jtfSpace.getText() + "6");
}
});
bSeven.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
jtfSpace.setText(jtfSpace.getText() + "7");
}
});
bEight.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
jtfSpace.setText(jtfSpace.getText() + "8");
}
});
bNine.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
jtfSpace.setText(jtfSpace.getText() + "9");
}
});
bZero.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
jtfSpace.setText(jtfSpace.getText() + "0");
}
});
bDot.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
jtfSpace.setText(jtfSpace.getText() + ".");
}
});
bAdd.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
math = "Add";
value = jtfSpace.getText();
num1 = Double.parseDouble(value);
jtfSpace.setText("");
}
});
bSub.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
math = "Sub";
value = jtfSpace.getText();
num1 = Double.parseDouble(value);
jtfSpace.setText("");
}
});
bMul.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
math = "Mul";
value = jtfSpace.getText();
num1 = Double.parseDouble(value);
jtfSpace.setText("");
}
});
bDiv.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
math = "Div";
value = jtfSpace.getText();
num1 = Double.parseDouble(value);
jtfSpace.setText("");
}
});
bEqual.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
value = jtfSpace.getText();
num2 = Double.parseDouble(value);
switch (math) {
case "Div" -> result = num1 / num2;
case "Mul" -> result = num1 * num2;
case "Add" -> result = num1 + num2;
case "Sub" -> result = num1 - num2;
}
jtfSpace.setText("" + result);
}
});
GridPane root=new GridPane();
HBox root2_1 = new HBox();
HBox root2_2 = new HBox();
HBox root2_3 = new HBox();
HBox root2_4 = new HBox();
Scene scene = new Scene(root,150,135);
root.addRow(0, jtfSpace);
root.addRow(1, root2_4);
root.addRow(2, root2_3);
root.addRow(3, root2_2);
root.addRow(4, root2_1);
root2_1.getChildren().addAll(bZero,bDot,bEqual,bAdd);
root2_2.getChildren().addAll(bOne,bTwo,bThree,bSub);
root2_3.getChildren().addAll(bFour,bFive,bSix,bMul);
root2_4.getChildren().addAll(bSeven,bEight,bNine,bDiv);
primaryStage.setScene(scene);
primaryStage.setTitle("CalculatorFX U1910060");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}