//Argument --module-path "C:javafx-sdk-20lib" --add-modules javafx.controls,javafx.fxml
// SimpleCalc
package application;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
// Create input fields
TextField t1 = new TextField();
TextField t2 = new TextField();
t1.setPromptText("Enter first number");
t2.setPromptText("Enter second number");
// Create buttons for operations
Button btnAdd = new Button("+");
Button btnSub = new Button("-");
Button btnMul = new Button("*");
Button btnDiv = new Button("/");
// Result label
Label lblRes = new Label("Result will be shown here");
btnAdd.setOnAction(e -> {
try {
double num1 = Double.parseDouble(t1.getText());
double num2 = Double.parseDouble(t2.getText());
lblRes.setText("Result: " + (num1 + num2));
} catch (Exception ex) {
lblRes.setText("Invalid Input!");
}
});
// btnDiv.setOnAction(new EventHandler<ActionEvent>() {
// @Override
// public void handle(ActionEvent e) {
// // your code here
// }
// });
btnSub.setOnAction(e -> {
try {
double num1 = Double.parseDouble(t1.getText());
double num2 = Double.parseDouble(t2.getText());
lblRes.setText("Result: " + (num1 - num2));
} catch (Exception ex) {
lblRes.setText("Invalid Input!");
}
});
btnMul.setOnAction(e -> {
try {
double num1 = Double.parseDouble(t1.getText());
double num2 = Double.parseDouble(t2.getText());
lblRes.setText("Result: " + (num1 * num2));
} catch (Exception ex) {
lblRes.setText("Invalid Input!");
}
});
btnDiv.setOnAction(e -> {
try {
double num1 = Double.parseDouble(t1.getText());
double num2 = Double.parseDouble(t2.getText());
if (num2 != 0)
lblRes.setText("Result: " + (num1 / num2));
else
lblRes.setText("Cannot divide by zero!");
} catch (Exception ex) {
lblRes.setText("Invalid Input!");
}
});
// Layouts
HBox inputs = new HBox(20, t1, t2);
inputs.setAlignment(Pos.CENTER);
HBox buttons = new HBox(30, btnAdd, btnSub, btnMul, btnDiv);
buttons.setAlignment(Pos.CENTER);
VBox root = new VBox(15, inputs, buttons, lblRes);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(20));
Scene scene = new Scene(root, 400, 450);
primaryStage.setTitle("Arithmetic Operations");
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

Simple Calculator using JavaFx a part of Advance Java

  • 1.
    //Argument --module-path "C:javafx-sdk-20lib"--add-modules javafx.controls,javafx.fxml // SimpleCalc package application; import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.scene.paint.Color; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage primaryStage) { try { // Create input fields TextField t1 = new TextField(); TextField t2 = new TextField(); t1.setPromptText("Enter first number"); t2.setPromptText("Enter second number"); // Create buttons for operations Button btnAdd = new Button("+"); Button btnSub = new Button("-"); Button btnMul = new Button("*"); Button btnDiv = new Button("/"); // Result label Label lblRes = new Label("Result will be shown here"); btnAdd.setOnAction(e -> { try { double num1 = Double.parseDouble(t1.getText()); double num2 = Double.parseDouble(t2.getText()); lblRes.setText("Result: " + (num1 + num2)); } catch (Exception ex) { lblRes.setText("Invalid Input!"); } }); // btnDiv.setOnAction(new EventHandler<ActionEvent>() { // @Override // public void handle(ActionEvent e) { // // your code here // } // }); btnSub.setOnAction(e -> { try { double num1 = Double.parseDouble(t1.getText()); double num2 = Double.parseDouble(t2.getText()); lblRes.setText("Result: " + (num1 - num2));
  • 2.
    } catch (Exceptionex) { lblRes.setText("Invalid Input!"); } }); btnMul.setOnAction(e -> { try { double num1 = Double.parseDouble(t1.getText()); double num2 = Double.parseDouble(t2.getText()); lblRes.setText("Result: " + (num1 * num2)); } catch (Exception ex) { lblRes.setText("Invalid Input!"); } }); btnDiv.setOnAction(e -> { try { double num1 = Double.parseDouble(t1.getText()); double num2 = Double.parseDouble(t2.getText()); if (num2 != 0) lblRes.setText("Result: " + (num1 / num2)); else lblRes.setText("Cannot divide by zero!"); } catch (Exception ex) { lblRes.setText("Invalid Input!"); } }); // Layouts HBox inputs = new HBox(20, t1, t2); inputs.setAlignment(Pos.CENTER); HBox buttons = new HBox(30, btnAdd, btnSub, btnMul, btnDiv); buttons.setAlignment(Pos.CENTER); VBox root = new VBox(15, inputs, buttons, lblRes); root.setAlignment(Pos.CENTER); root.setPadding(new Insets(20)); Scene scene = new Scene(root, 400, 450); primaryStage.setTitle("Arithmetic Operations"); primaryStage.setScene(scene); primaryStage.show(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { launch(args); } }