-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculate.java
More file actions
89 lines (76 loc) · 2.87 KB
/
Calculate.java
File metadata and controls
89 lines (76 loc) · 2.87 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
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
public class Calculate extends Application {
private TextField tf1, tf2;
private Text operator, result;
private RadioButton rbMultiply, rbDivide;
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
root.setPadding(new Insets(20));
HBox inputBox = new HBox(10);
tf1 = new TextField();
tf2 = new TextField();
operator = new Text(" or ");
inputBox.getChildren().addAll(tf1, operator, tf2);
root.setTop(inputBox);
VBox radioBox = new VBox(10);
rbMultiply = new RadioButton("*");
rbDivide = new RadioButton("/");
ToggleGroup group = new ToggleGroup();
rbMultiply.setToggleGroup(group);
rbDivide.setToggleGroup(group);
rbMultiply.setOnAction(e -> operator.setText("*"));
rbDivide.setOnAction(e -> operator.setText("/"));
radioBox.getChildren().addAll(rbMultiply, rbDivide);
root.setCenter(radioBox);
// Result Text and Calculate Button
result = new Text();
Button calculateBtn = new Button("Calculate");
calculateBtn.setOnAction(new CalculateBtHandler());
VBox resultBox = new VBox(10, result, calculateBtn);
root.setBottom(resultBox);
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle("Simple Calculator");
primaryStage.setScene(scene);
primaryStage.show();
}
class CalculateBtHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
try {
double num1 = Double.parseDouble(tf1.getText());
double num2 = Double.parseDouble(tf2.getText());
double answer;
if (operator.getText().equals("*")) {
answer = num1 * num2;
result.setText(String.valueOf(answer));
} else if (operator.getText().equals("/")) {
if (num2 == 0) {
result.setText("Error");
return;
}
answer = num1 / num2;
result.setText(String.valueOf(answer));
}
} catch (NumberFormatException ex) {
result.setText("Error");
}
}
}
public static void main(String[] args) {
launch(args);
}
}