JavaFX Calculator Application: Complete Source Code
JavaFX Calculator Implementation
This implementation defines a desktop calculator application built using JavaFX, utilizing a BorderPane and GridPane to manage the user interface components.
Complete JavaFX Source Code
import javafx.application.*;
import javafx.stage.*;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.input.*;
import javafx.scene.paint.Color;
public class Main extends Application {
int operation = -1;
double data = 0;
private TextField Resultado;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Calculadora");
Group G = new Group();
Scene SC = new Scene(G);
primaryStage.setScene(SC);
SC.setFill(Color.BLACK);
Resultado = new TextField();
Resultado.setPrefSize(200, 60);
BorderPane bp = new BorderPane();
bp.setTop(Resultado);
GridPane GP = new GridPane();
bp.setCenter(GP);
G.getChildren().add(bp);
Button B1 = new Button("1");
B1.setDefaultButton(true);
B1.setPrefSize(50, 50);
B1.setStyle("-fx-background-insets: 0,1,4,5,6");
B1.setStyle("-fx-background-radius: 9,8,5,4,3");
GP.add(B1, 0, 0);
B1.addEventHandler(MouseEvent.MOUSE_CLICKED, new BotonHandler(new Tecla("1")));
Button B2 = new Button("2");
B2.setDefaultButton(true);
B2.setPrefSize(50, 50);
B2.setStyle("-fx-background-insets: 0,1,4,5,6");
B2.setStyle("-fx-background-radius: 9,8,5,4,3");
GP.add(B2, 1, 0);
B2.addEventHandler(MouseEvent.MOUSE_CLICKED, new BotonHandler(new Tecla("2")));
Button B3 = new Button("3");
B3.setDefaultButton(true);
B3.setPrefSize(50, 50);
B3.setStyle("-fx-background-insets: 0,1,4,5,6");
B3.setStyle("-fx-background-radius: 9,8,5,4,3");
GP.add(B3, 2, 0);
B3.addEventHandler(MouseEvent.MOUSE_CLICKED, new BotonHandler(new Tecla("3")));
Button B4 = new Button("4");
B4.setDefaultButton(true);
B4.setPrefSize(50, 50);
B4.setStyle("-fx-background-insets: 0,1,4,5,6");
B4.setStyle("-fx-background-radius: 9,8,5,4,3");
GP.add(B4, 0, 1);
B4.addEventHandler(MouseEvent.MOUSE_CLICKED, new BotonHandler(new Tecla("4")));
Button B5 = new Button("5");
B5.setDefaultButton(true);
B5.setPrefSize(50, 50);
B5.setStyle("-fx-background-insets: 0,1,4,5,6");
B5.setStyle("-fx-background-radius: 9,8,5,4,3");
GP.add(B5, 1, 1);
B5.addEventHandler(MouseEvent.MOUSE_CLICKED, new BotonHandler(new Tecla("5")));
Button B6 = new Button("6");
B6.setDefaultButton(true);
B6.setPrefSize(50, 50);
B6.setStyle("-fx-background-insets: 0,1,4,5,6");
B6.setStyle("-fx-background-radius: 9,8,5,4,3");
GP.add(B6, 2, 1);
B6.addEventHandler(MouseEvent.MOUSE_CLICKED, new BotonHandler(new Tecla("6")));
Button B7 = new Button("7");
B7.setDefaultButton(true);
B7.setPrefSize(50, 50);
B7.setStyle("-fx-background-insets: 0,1,4,5,6");
B7.setStyle("-fx-background-radius: 9,8,5,4,3");
GP.add(B7, 0, 2);
B7.addEventHandler(MouseEvent.MOUSE_CLICKED, new BotonHandler(new Tecla("7")));
Button B8 = new Button("8");
B8.setDefaultButton(true);
B8.setPrefSize(50, 50);
B8.setStyle("-fx-background-insets: 0,1,4,5,6");
B8.setStyle("-fx-background-radius: 9,8,5,4,3");
GP.add(B8, 1, 2);
B8.addEventHandler(MouseEvent.MOUSE_CLICKED, new BotonHandler(new Tecla("8")));
Button B9 = new Button("9");
B9.setDefaultButton(true);
B9.setPrefSize(50, 50);
B9.setStyle("-fx-background-insets: 0,1,4,5,6");
B9.setStyle("-fx-background-radius: 9,8,5,4,3");
GP.add(B9, 2, 2);
B9.addEventHandler(MouseEvent.MOUSE_CLICKED, new BotonHandler(new Tecla("9")));
Button B0 = new Button("0");
B0.setDefaultButton(true);
B0.setPrefSize(50, 50);
B0.setStyle("-fx-background-insets: 0,1,4,5,6");
B0.setStyle("-fx-background-radius: 9,8,5,4,3");
GP.add(B0, 1, 3);
B0.addEventHandler(MouseEvent.MOUSE_CLICKED, new BotonHandler(new Tecla("0")));
Button B10 = new Button("CE");
B10.setDefaultButton(true);
B10.setPrefSize(50, 50);
B10.setStyle("-fx-background-insets: 0,1,4,5,6");
B10.setStyle("-fx-background-radius: 9,8,5,4,3");
GP.add(B10, 0, 3);
B10.addEventHandler(MouseEvent.MOUSE_CLICKED, new BotonHandler(new Tecla("CE")));
Button B11 = new Button("=");
B11.setDefaultButton(true);
B11.setPrefSize(50, 50);
B11.setStyle("-fx-background-insets: 0,1,4,5,6");
B11.setStyle("-fx-background-radius: 9,8,5,4,3");
GP.add(B11, 2, 3);
B11.addEventHandler(MouseEvent.MOUSE_CLICKED, new BotonHandler(new Tecla("=")));
Button B12 = new Button("+");
B12.setDefaultButton(true);
B12.setPrefSize(50, 50);
B12.setStyle("-fx-background-insets: 0,1,4,5,6");
B12.setStyle("-fx-background-radius: 9,8,5,4,3");
GP.add(B12, 3, 0);
B12.addEventHandler(MouseEvent.MOUSE_CLICKED, new BotonHandler(new Tecla("+")));
Button B13 = new Button("-");
B13.setDefaultButton(true);
B13.setPrefSize(50, 50);
B13.setStyle("-fx-background-insets: 0,1,4,5,6");
B13.setStyle("-fx-background-radius: 9,8,5,4,3");
GP.add(B13, 3, 1);
B13.addEventHandler(MouseEvent.MOUSE_CLICKED, new BotonHandler(new Tecla("-")));
Button B14 = new Button("/");
B14.setDefaultButton(true);
B14.setPrefSize(50, 50);
B14.setStyle("-fx-background-insets: 0,1,4,5,6");
B14.setStyle("-fx-background-radius: 9,8,5,4,3");
GP.add(B14, 3, 2);
B14.addEventHandler(MouseEvent.MOUSE_CLICKED, new BotonHandler(new Tecla("/")));
Button B15 = new Button("x");
B15.setDefaultButton(true);
B15.setPrefSize(50, 50);
B15.setStyle("-fx-background-insets: 0,1,4,5,6");
B15.setStyle("-fx-background-radius: 9,8,5,4,3");
GP.add(B15, 3, 3);
B15.addEventHandler(MouseEvent.MOUSE_CLICKED, new BotonHandler(new Tecla("x")));
primaryStage.show();
}
public class Tecla {
public String tecla;
public Tecla(String tecla) {
this.tecla = tecla;
}
}
public class BotonHandler implements EventHandler<MouseEvent> {
public Tecla presionado;
public BotonHandler(Tecla presionado) {
this.presionado = presionado;
}
@Override
public void handle(MouseEvent E) {
if (presionado.tecla.equals("+")) {
data = Double.parseDouble(Resultado.getText());
operation = 1;
Resultado.setText("");
} else if (presionado.tecla.equals("-")) {
data = Double.parseDouble(Resultado.getText());
operation = 2;
Resultado.setText("");
} else if (presionado.tecla.equals("x")) {
data = Double.parseDouble(Resultado.getText());
operation = 3;
Resultado.setText("");
} else if (presionado.tecla.equals("/")) {
data = Double.parseDouble(Resultado.getText());
operation = 4;
Resultado.setText("");
} else if (presionado.tecla.equals("CE")) {
Resultado.setText("");
} else if (presionado.tecla.equals("=")) {
double dataF = Double.parseDouble(Resultado.getText());
switch (operation) {
case 1:
double ans = (data + dataF);
Resultado.setText(String.valueOf(ans));
break;
case 2:
ans = (data - dataF);
Resultado.setText(String.valueOf(ans));
break;
case 3:
ans = (data * dataF);
Resultado.setText(String.valueOf(ans));
break;
case 4:
ans = (data / dataF);
Resultado.setText(String.valueOf(ans));
break;
default:
Resultado.setText("error");
break;
}
} else {
Resultado.setText(Resultado.getText() + presionado.tecla);
}
}
}
}