Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package application;
- import javafx.application.Application;
- import javafx.beans.binding.Bindings;
- import javafx.event.ActionEvent;
- import javafx.event.Event;
- import javafx.event.EventHandler;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.stage.Stage;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.FlowPane;
- public class My_Calculator extends Application {
- @Override
- public void start(Stage primaryStage) {
- FlowPane pane = new FlowPane();
- pane.setAlignment(Pos.CENTER);
- pane.setPadding(new Insets ( 30 , 20 , 30 , 20));
- pane.setHgap(5);
- pane.setVgap(5);
- pane.setMinWidth(220);
- pane.setPrefWidth(220);
- pane.setMaxWidth(220);;
- TextField Result = new TextField();
- Result.setEditable(false); //Don't allow the TextField to be editable.
- Result.setAlignment(Pos.CENTER_RIGHT);
- Result.setMinSize(210, 30);
- pane.getChildren().add(Result);
- Button siete = new Button("7");
- Button ocho = new Button("8");
- Button nueve = new Button("9");
- Button mas = new Button("+");
- siete.setMinSize(40, 40);
- ocho.setMinSize(40, 40);
- nueve.setMinSize(40, 40);
- mas.setMinSize(40, 40);
- pane.getChildren().add(siete);
- pane.getChildren().add(ocho);
- pane.getChildren().add(nueve);
- pane.getChildren().add(mas);
- //Create next row
- Button cuatro = new Button("4");
- Button cinco = new Button("5");
- Button seis = new Button("6");
- Button menos = new Button("-");
- cuatro.setMinSize(40, 40);
- cinco.setMinSize(40, 40);
- seis.setMinSize(40, 40);
- menos.setMinSize(40, 40);
- pane.getChildren().add(cuatro);
- pane.getChildren().add(cinco);
- pane.getChildren().add(seis);
- pane.getChildren().add(menos);
- //Create next row
- Button uno = new Button("1");
- Button dos = new Button("2");
- Button tres = new Button("3");
- Button multiplicar = new Button("*");
- uno.setMinSize(40, 40);
- dos.setMinSize(40, 40);
- tres.setMinSize(40, 40);
- multiplicar.setMinSize(40, 40);
- pane.getChildren().add(uno);
- pane.getChildren().add(dos);
- pane.getChildren().add(tres);
- pane.getChildren().add(multiplicar);
- //Final Row
- Button zero = new Button("0");
- Button igual = new Button("=");
- Button dividir = new Button("/");
- Button C = new Button ("C");
- zero.setMinSize(40, 40);
- igual.setMinSize(40, 40);
- dividir.setMinSize(40, 40);
- C.setMinSize(40, 40);
- pane.getChildren().add(zero);
- pane.getChildren().add(C);
- pane.getChildren().add(igual);
- pane.getChildren().add(dividir);
- //Create event handler
- @SuppressWarnings("rawtypes")
- EventHandler handle = new EventHandler() {
- @Override
- public void handle(Event event)
- {
- Button tempButton = ((Button) event.getSource());//Get the Button that is being pressed
- String currentButtonPress = tempButton.getText();//Get the text of the button that is being pressed
- String tempString = null;
- int tempInt = 0;
- int tempInt2 = 0;
- int sum= 0;
- //Depending on the Button text do some action
- switch (currentButtonPress) {
- case "=":
- tempString = Result.getText();
- tempInt2 = Integer.parseInt(tempString);
- sum = tempInt+tempInt2;
- tempString = String.valueOf(sum);
- Result.appendText(tempString);
- break;
- case "+":
- tempString = Result.getText();
- System.out.println(tempString);
- tempInt = Integer.parseInt(tempString);
- Result.clear();
- break;
- case "C":
- Result.clear();
- Result.setText("0");
- default:
- Result.appendText(currentButtonPress);
- }
- }
- };
- //Set buttons' handlers;
- uno.setOnAction(handle);
- dos.setOnAction(handle);
- tres.setOnAction(handle);
- cuatro.setOnAction(handle);
- cinco.setOnAction(handle);
- seis.setOnAction(handle);
- siete.setOnAction(handle);
- ocho.setOnAction(handle);
- nueve.setOnAction(handle);
- menos.setOnAction(handle);
- mas.setOnAction(handle);
- multiplicar.setOnAction(handle);
- dividir.setOnAction(handle);
- igual.setOnAction(handle);
- C.setOnAction(handle);
- // Show Scene
- Scene scene = new Scene(pane);
- primaryStage.setTitle("Calc.");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement