Java Reference
In-Depth Information
Next, you'll need to add an action handler to the button. Again, the basic concepts are the same—
you need to catch interesting UI events and respond to them:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.scene.layout.HBox;
import javafx.stage.Modality;
import javafx.stage.StageStyle;
public class BMICalculatorFX extends Application {
private Stage stage;
private final TextField txtMass = new TextField();
private final TextField txtHeight = new TextField();
private final Button btnCalc = new Button("Calculate BMI");
@Override
public void start(Stage stage) throws Exception {
this.stage = stage;
VBox vbox = new VBox(10);
Label lblTitle = new Label("BMI Calculator");
lblTitle.setFont(Font.font(18));
vbox.getChildren().add(lblTitle);
vbox.getChildren().add(new Label("Your mass (kg):"));
vbox.getChildren().add(txtMass);
vbox.getChildren().add(new Label("Your height (cm):"));
vbox.getChildren().add(txtHeight);
vbox.getChildren().add(btnCalc);
btnCalc.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent ev) {
double mass;
double height;
try {
mass = Double.parseDouble(txtMass.getText());
height = Double.parseDouble(txtHeight.getText());
} catch (NumberFormatException e) {
showMessage("Check your input.", "Error in number input");
Search WWH ::




Custom Search