Java Reference
In-Depth Information
public void actionPerformed(ActionEvent arg0) {
double mass;
double height;
try {
mass = Double.parseDouble(txtMass.getText());
height = Double.parseDouble(txtHeight.getText());
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(self,
"Please enter a valid number for mass and height.",
"Input error",
JOptionPane.ERROR_MESSAGE);
return;
}
double result = calculateBMI(mass, height);
JOptionPane.showMessageDialog(self,
"Your BMI is: " + (Math.round(result*100.0)/100.0),
"Your BMI result",
JOptionPane.PLAIN_MESSAGE);
}
});
*/
}
@Override
public void start(Stage stage) throws Exception {
Scene scene = new Scene(new Group());
stage.setTitle("JavaFX BMI Calculator");
stage.setScene(scene);
stage.sizeToScene();
stage.show();
}
protected double calculateBMI(double mass, double height) {
return mass / Math.pow(height/100.0, 2.0);
}
public static void main(String[] args) {
Application.launch(args);
}
}
Note that Eclipse will complain about your JavaFX imports. The reason for this is that they are only part
of Oracle's JRE and other Java implementations might not support this. To solve this warning, right‐click
your project in the Package Explorer, select Build Path followed by Configure Build Path. In the window
that opens, navigate to the Libraries tab and expand the entry for JRE System Library. Select Access Rules
and click Edit. Next, add an access rule to make javafx/** accessible, as illustrated in Figure 11-31.
Press OK. Your build path window should now look like Figure 11-32.
Press OK again. Your project will recompile and the errors should disappear.
You can try running the project now. A window will appear, but it seems to contain absolutely noth-
ing, which is not surprising seeing that you set up an empty scene for the stage:
Scene scene = new Scene(new Group());
Search WWH ::




Custom Search