-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
94 lines (80 loc) · 3.27 KB
/
Calculator.java
File metadata and controls
94 lines (80 loc) · 3.27 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
/**
* Adnaan Chida
* Purpose: Create a simple calculator
* Write a program to perform addition, subtraction, multiplication,
* and division
*/
// import java packages
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Lab16 extends Application{
// variables for the calculator
double number1 = 0;
double number2 = 0;
double answer = 0;
@Override
// to create a seperate dialog box
public void start(Stage primaryStage) throws Exception {
HBox numberPane = new HBox();
numberPane.setSpacing(10);
numberPane.setAlignment(Pos.CENTER);
Label lblNumber1 = new Label("Number 1:");
TextField tfNumber1 = new TextField();
Label lblNumber2 = new Label("Number 2:");
TextField tfNumber2 = new TextField();
Label lblResult = new Label("Result :");
TextField tfResult = new TextField();
numberPane.getChildren().addAll(
lblNumber1, tfNumber1,
lblNumber2, tfNumber2,
lblResult, tfResult);
// button to calculate
// add button
Button btAdd = new Button("Add");
btAdd.setOnAction(e -> {
answer = Double.parseDouble(tfNumber1.getText()) + Double.parseDouble(tfNumber2.getText());
tfResult.setText(answer + "");
});
// subtract button
Button btSubtract = new Button("subtract");
btSubtract.setOnAction(e -> {
answer = Double.parseDouble(tfNumber1.getText()) - Double.parseDouble(tfNumber2.getText());
tfResult.setText(answer + "");
});
// multiply button
Button btMultiply = new Button("Multiply");
btMultiply.setOnAction(e -> {
answer = Double.parseDouble(tfNumber1.getText()) * Double.parseDouble(tfNumber2.getText());
tfResult.setText(answer + "");
});
// Divide button
Button btDivide = new Button("Divide");
btDivide.setOnAction(e -> {
answer = Double.parseDouble(tfNumber1.getText()) / Double.parseDouble(tfNumber2.getText());
tfResult.setText(answer + "");
});
HBox operatorsPane = new HBox();
operatorsPane.setSpacing(10);
operatorsPane.setAlignment(Pos.CENTER);
operatorsPane.getChildren().addAll(btAdd, btSubtract, btMultiply, btDivide);
BorderPane borderPane = new BorderPane(numberPane);
BorderPane.setMargin(numberPane, new Insets(10, 10, 10, 10));
borderPane.setBottom(operatorsPane);
BorderPane.setMargin(operatorsPane, new Insets(10, 10, 10, 10));
primaryStage.setScene(new Scene(borderPane, borderPane.getPrefWidth(),borderPane.getPrefWidth()));
primaryStage.setTitle("Simple calculator");
primaryStage.show();
}
// main class, launches the program
public static void main(String[] args) {
Application.launch(args);
}
}