Create a JavaFx application which implement different mathematical operation as indicated: GUI Calculator Enter first number (x): Enter second number (y): Output: Calculate GUI Calculator 0 X Reset GUI Calculator Enter first number (x) Enter second number (y): Output GUI Calculator 1 Calculate Reset
below is my code, need help with this javafx code
package com.example.demo1;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Objects;
import java.util.Optional;
public class HelloApplication extends Application {
public boolean inputValidation(String s){
try {
int a = Integer.parseInt(s);
return true;
}catch (Exception e ){
return false;
}
}
@Override
public void start(Stage stage) throws IOException {
GridPane grid= new GridPane();
grid.setVgap(10);
grid.setHgap(10);
//Component
RadioButton opt1 = new RadioButton("*");
RadioButton opt2 = new RadioButton("+");
RadioButton opt3 = new RadioButton("-");
RadioButton opt4 = new RadioButton("/");
Label weightL = new Label("Enter first number(x)");
Label heightL = new Label("Enter second number(y)");
Label bmiL = new Label("GUI");
TextField hText = new TextField();
TextField wText = new TextField();
TextField bText = new TextField();
Button btn = new Button("Calculate!");
HBox toggle = new HBox();
ToggleGroup tg = new ToggleGroup();
opt1.setToggleGroup(tg);
opt2.setToggleGroup(tg);
opt3.setToggleGroup(tg);
opt4.setToggleGroup(tg);
toggle.getChildren().addAll(opt1,opt2,opt3,opt4);
grid.add(weightL,0,0);
grid.add(wText,1,0);
grid.add(heightL,0,1);
grid.add(hText,1,1);
grid.add(toggle,0,2);
grid.add(btn,0,3);
grid.add(bmiL,0,4);
grid.add(bText,1,4);
btn.setOnAction(event -> {
if(inputValidation(hText.getText())&&inputValidation(wText.getText())) {
RadioButton gg = (RadioButton) tg.getSelectedToggle();
if (gg == null) {
Alert a = new Alert(Alert.AlertType.ERROR);
a.setTitle("Error");
a.setHeaderText("Nothing is Selected");
a.setContentText("Please select the option out of English or Metric");
a.show();
} else {
double weight = Double.parseDouble(wText.getText());
double height = Double.parseDouble(hText.getText());
if (gg.getText() == "*") {
double BMI = (weight / Math.pow(height,2)) * 703;
bText.setText(Double.toString(BMI));
} else if (gg.getText() == "+") {
double BMI = ((weight) / Math.pow(height,2))*10000;
bText.setText(Double.toString(BMI));
}
}
}else {
Alert a = new Alert(Alert.AlertType.ERROR);
a.setTitle("Error");
a.setHeaderText("Invalid Input");
a.setContentText("Please check Your input maybe br it is invalid");
a.show();
}
});
Scene scene = new Scene(grid);
stage.setWidth(400);
stage.setHeight(400);
stage.setTitle("BMI Calculator");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}


Step by step
Solved in 3 steps with 2 images









