Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.event.ActionEvent;
  3. import javafx.event.EventHandler;
  4. import javafx.geometry.Insets;
  5. import javafx.geometry.Pos;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.Button;
  8. import javafx.scene.control.TextField;
  9. import javafx.scene.layout.BorderPane;
  10. import javafx.scene.layout.HBox;
  11. import javafx.stage.Stage;
  12.  
  13. public class Calculator extends Application {
  14.  
  15. public static void main(String[] args) {
  16. launch(args);
  17. }
  18.  
  19. @Override
  20. public void start(Stage primaryStage) {
  21. primaryStage.setTitle("Simple Calculator");
  22.  
  23. // declare a border pane
  24. BorderPane borderPane = new BorderPane();
  25.  
  26. // declare some HBoxes (horizontal boxes)
  27. HBox topHBox = new HBox();
  28. topHBox.setAlignment(Pos.CENTER);
  29. topHBox.setPadding(new Insets(10, 10, 10, 10));
  30. topHBox.setSpacing(20);
  31.  
  32. HBox middleHBox = new HBox();
  33. middleHBox.setAlignment(Pos.CENTER);
  34. middleHBox.setPadding(new Insets(10, 10, 10, 10));
  35. middleHBox.setSpacing(20);
  36.  
  37. HBox bottomHBox = new HBox();
  38. bottomHBox.setAlignment(Pos.CENTER);
  39. bottomHBox.setPadding(new Insets(10, 10, 10, 10));
  40. bottomHBox.setSpacing(20);
  41.  
  42. //declare all the controls (text fields and buttons)
  43. TextField input1TF = new TextField();
  44. input1TF.setAlignment(Pos.CENTER_RIGHT);
  45.  
  46. TextField input2TF = new TextField();
  47. input2TF.setAlignment(Pos.CENTER_RIGHT);
  48.  
  49. TextField resultTF = new TextField();
  50. resultTF.setAlignment(Pos.CENTER_RIGHT);
  51.  
  52. Button addButton = new Button("+");
  53. addButton.setPrefSize(30,30);
  54.  
  55. Button subButton = new Button("-");
  56. subButton.setPrefSize(30,30);
  57.  
  58. Button multButton = new Button("X");
  59. multButton.setPrefSize(30,30);
  60.  
  61. Button divButton = new Button("/");
  62. divButton.setPrefSize(30,30);
  63.  
  64.  
  65. // add listeners to the buttons
  66. addButton.setOnAction(new EventHandler<ActionEvent>() {
  67. @Override
  68. public void handle(ActionEvent event) {
  69. addNumbers(input1TF, input2TF, resultTF);
  70. }
  71. });
  72.  
  73. subButton.setOnAction(new EventHandler<ActionEvent>() {
  74. @Override
  75. public void handle(ActionEvent event) {
  76. subtractNumbers(input1TF, input2TF, resultTF);
  77. }
  78. });
  79.  
  80. multButton.setOnAction(new EventHandler<ActionEvent>() {
  81. @Override
  82. public void handle(ActionEvent event) {
  83. multiplyNumbers(input1TF, input2TF, resultTF);
  84. }
  85. });
  86.  
  87. divButton.setOnAction(new EventHandler<ActionEvent>() {
  88. @Override
  89. public void handle(ActionEvent event) {
  90. divideNumbers(input1TF, input2TF, resultTF);
  91. }
  92. });
  93.  
  94. // add the controls to the boxes
  95. topHBox.getChildren().addAll(input1TF, input2TF);
  96. middleHBox.getChildren().addAll(addButton, subButton, multButton, divButton);
  97. bottomHBox.getChildren().add(resultTF);
  98.  
  99. // add the boxes to the border layout
  100. borderPane.setTop(topHBox);
  101. borderPane.setCenter(middleHBox);
  102. borderPane.setBottom(bottomHBox);
  103.  
  104. // add the border layout to a scene and set this on the primary stage
  105. Scene scene = new Scene(borderPane, 350, 150);
  106. primaryStage.setScene(scene);
  107. primaryStage.show();
  108.  
  109. }
  110.  
  111. private void addNumbers(TextField input1TF, TextField input2TF, TextField resultTF) {
  112. try {
  113. double d1 = Double.parseDouble(input1TF.getText());
  114. double d2 = Double.parseDouble(input2TF.getText());
  115.  
  116. double result = d1 + d2;
  117.  
  118. resultTF.setText("" + result);
  119. } catch (NumberFormatException e) {
  120. resultTF.setText("Not a Number");
  121. }
  122. }
  123.  
  124. private void subtractNumbers(TextField input1TF, TextField input2TF, TextField resultTF) {
  125. try {
  126. double d1 = Double.parseDouble(input1TF.getText());
  127. double d2 = Double.parseDouble(input2TF.getText());
  128.  
  129. double result = d1 - d2;
  130.  
  131. resultTF.setText("" + result);
  132. } catch (NumberFormatException e) {
  133. resultTF.setText("Not a Number");
  134. }
  135. }
  136.  
  137. private void multiplyNumbers(TextField input1TF, TextField input2TF, TextField resultTF) {
  138. try {
  139. double d1 = Double.parseDouble(input1TF.getText());
  140. double d2 = Double.parseDouble(input2TF.getText());
  141.  
  142. double result = d1 * d2;
  143.  
  144. resultTF.setText("" + result);
  145. } catch (NumberFormatException e) {
  146. resultTF.setText("Not a Number");
  147. }
  148. }
  149.  
  150. private void divideNumbers(TextField input1TF, TextField input2TF, TextField resultTF) {
  151. try {
  152. double d1 = Double.parseDouble(input1TF.getText());
  153. double d2 = Double.parseDouble(input2TF.getText());
  154.  
  155. double result = d1 / d2;
  156.  
  157. resultTF.setText("" + result);
  158. } catch (NumberFormatException e) {
  159. resultTF.setText("Not a Number");
  160. }
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement