Advertisement
Afern247

Calculator

Aug 20th, 2017
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.35 KB | None | 0 0
  1. package application;
  2.    
  3. import javafx.application.Application;
  4. import javafx.beans.binding.Bindings;
  5. import javafx.event.ActionEvent;
  6. import javafx.event.Event;
  7. import javafx.event.EventHandler;
  8. import javafx.geometry.Insets;
  9. import javafx.geometry.Pos;
  10. import javafx.stage.Stage;
  11. import javafx.scene.Scene;
  12. import javafx.scene.control.Button;
  13. import javafx.scene.control.TextField;
  14. import javafx.scene.layout.BorderPane;
  15. import javafx.scene.layout.FlowPane;
  16.  
  17.  
  18. public class My_Calculator extends Application {
  19.     @Override
  20.    
  21.    
  22.     public void start(Stage primaryStage) {
  23.  
  24.        
  25.  
  26.         FlowPane pane = new FlowPane();
  27.         pane.setAlignment(Pos.CENTER);
  28.         pane.setPadding(new Insets ( 30 , 20 , 30 , 20));
  29.         pane.setHgap(5);
  30.         pane.setVgap(5);
  31.         pane.setMinWidth(220);  
  32.         pane.setPrefWidth(220);
  33.         pane.setMaxWidth(220);;
  34.  
  35.         TextField Result = new TextField();
  36.         Result.setEditable(false);              //Don't allow the TextField to be editable.
  37.         Result.setAlignment(Pos.CENTER_RIGHT);
  38.         Result.setMinSize(210, 30);
  39.        
  40.         pane.getChildren().add(Result);
  41.        
  42.  
  43.  
  44.  
  45.         Button siete = new Button("7");          
  46.         Button ocho = new Button("8");
  47.         Button nueve = new Button("9");
  48.         Button mas = new Button("+");
  49.  
  50.         siete.setMinSize(40, 40);
  51.         ocho.setMinSize(40, 40);
  52.         nueve.setMinSize(40, 40);
  53.         mas.setMinSize(40, 40);
  54.  
  55.         pane.getChildren().add(siete);
  56.         pane.getChildren().add(ocho);
  57.         pane.getChildren().add(nueve);
  58.         pane.getChildren().add(mas);
  59.  
  60.  
  61.  
  62.         //Create next row
  63.  
  64.         Button cuatro = new Button("4");
  65.         Button cinco = new Button("5");
  66.         Button seis = new Button("6");
  67.         Button menos = new Button("-");
  68.  
  69.         cuatro.setMinSize(40, 40);
  70.         cinco.setMinSize(40, 40);
  71.         seis.setMinSize(40, 40);
  72.         menos.setMinSize(40, 40);
  73.  
  74.         pane.getChildren().add(cuatro);
  75.         pane.getChildren().add(cinco);
  76.         pane.getChildren().add(seis);
  77.         pane.getChildren().add(menos);
  78.  
  79.  
  80.  
  81.         //Create next row
  82.  
  83.  
  84.         Button uno = new Button("1");
  85.         Button dos = new Button("2");
  86.         Button tres = new Button("3");
  87.         Button multiplicar = new Button("*");
  88.  
  89.         uno.setMinSize(40, 40);
  90.         dos.setMinSize(40, 40);
  91.         tres.setMinSize(40, 40);
  92.         multiplicar.setMinSize(40, 40);
  93.  
  94.         pane.getChildren().add(uno);
  95.         pane.getChildren().add(dos);
  96.         pane.getChildren().add(tres);
  97.         pane.getChildren().add(multiplicar);
  98.  
  99.         //Final Row
  100.  
  101.         Button zero = new Button("0");
  102.         Button igual = new Button("=");
  103.         Button dividir = new Button("/");
  104.         Button C = new Button ("C");
  105.  
  106.         zero.setMinSize(40, 40);
  107.         igual.setMinSize(40, 40);
  108.         dividir.setMinSize(40, 40);
  109.         C.setMinSize(40, 40);
  110.  
  111.         pane.getChildren().add(zero);
  112.         pane.getChildren().add(C);
  113.         pane.getChildren().add(igual);
  114.         pane.getChildren().add(dividir);
  115.  
  116.  
  117.  
  118.  
  119.       //Create event handler
  120.         @SuppressWarnings("rawtypes")
  121.         EventHandler handle = new EventHandler() {
  122.             @Override
  123.             public void handle(Event event)
  124.             {
  125.                 Button tempButton = ((Button) event.getSource());//Get the Button that is being pressed
  126.  
  127.                
  128.                
  129.                 String currentButtonPress = tempButton.getText();//Get the text of the button that is being pressed
  130.                 String tempString = null;
  131.                 int tempInt = 0;
  132.                 int tempInt2 = 0;
  133.                 int sum= 0;
  134.                
  135.                 //Depending on the Button text do some action
  136.                 switch (currentButtonPress) {
  137.                     case "=":
  138.                         tempString = Result.getText();
  139.                         tempInt2 = Integer.parseInt(tempString);
  140.                         sum = tempInt+tempInt2;
  141.                        
  142.                         tempString = String.valueOf(sum);
  143.                         Result.appendText(tempString);
  144.                         break;
  145.                     case "+":
  146.                         tempString = Result.getText();
  147.                         System.out.println(tempString);
  148.                         tempInt = Integer.parseInt(tempString);
  149.                         Result.clear();
  150.                        
  151.                         break;
  152.                     case "C":
  153.                         Result.clear();
  154.                         Result.setText("0");
  155.                     default:
  156.                         Result.appendText(currentButtonPress);
  157.                 }
  158.            
  159.                
  160.                
  161.             }
  162.  
  163.         };
  164.        
  165.        
  166.       //Set buttons' handlers;
  167.         uno.setOnAction(handle);
  168.         dos.setOnAction(handle);
  169.         tres.setOnAction(handle);
  170.         cuatro.setOnAction(handle);
  171.         cinco.setOnAction(handle);
  172.         seis.setOnAction(handle);
  173.         siete.setOnAction(handle);
  174.         ocho.setOnAction(handle);
  175.         nueve.setOnAction(handle);
  176.         menos.setOnAction(handle);
  177.         mas.setOnAction(handle);
  178.         multiplicar.setOnAction(handle);
  179.         dividir.setOnAction(handle);
  180.         igual.setOnAction(handle);
  181.         C.setOnAction(handle);
  182.        
  183.        
  184.  
  185.  
  186.        // Show Scene
  187.  
  188.         Scene scene = new Scene(pane);
  189.         primaryStage.setTitle("Calc.");
  190.         primaryStage.setScene(scene);
  191.         primaryStage.show();
  192.  
  193.  
  194.  
  195.     }
  196.  
  197.  
  198.  
  199.    
  200.    
  201.     public static void main(String[] args) {
  202.         launch(args);
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement