所以我一直在研究javaFX&Scenebuilder项目来创建BMI计算器,但似乎有点卡住了。关于我应该做什么的任何提示/建议?
其中一项要求是拥有两个交互式类以及控制器,fxml页面和应用启动器。第一类用于将英寸转换成厘米,将磅转换成千克,然后在第二类中用于计算bmi值和状态。
public class BMIInput {
private double heightIN;
private double heightCM;
private double weightKG;
private double weightLBS;
public double height;
public double weight;
public BMIInput() {
}
public double getHeight() {
if (heightIN == 0) {
height = heightCM;
} else if (heightCM == 0) {
height = (heightIN * 2.54);
}
return height;
}
public double getWeight() {
if (weightLBS == 0) {
weight = weightKG;
} else if (weightKG == 0) {
weight = (weightLBS / 2.205);
}
return weight;
}
public double setHeightIN(double height) {
return this.heightIN = height;
}
public double setHeightCM(double height) {
return this.heightCM = height;
}
public double setWeightKG(double weight) {
return this.weightKG = weight;
}
public double setWeightLBS(double weight) {
return this.weightLBS = weight;
}
}
第二堂课
public class BMICalculator {
BMIInput height;
BMIInput weight;
double bmiValue;
public double getBmiValue() {
return bmiValue;
}
public void setBmiValue(double bmiValue) {
this.bmiValue = bmiValue;
}
public double calculateBMI() {
double heightDouble = Double.parseDouble(height.toString());
double weightDouble = Double.parseDouble(weight.toString());
bmiValue = (weightDouble / (heightDouble / 100.0) * (heightDouble / 100));
return bmiValue;
}
public String calculateStatus() {
String value = "";
if (bmiValue <= 18.5) {
value = "Underweight";
} else if (18.6 <= bmiValue && bmiValue <= 24.9) {
value = "Normal weight";
} else if (25 <= bmiValue && bmiValue <= 29.9) {
value = "Overweight";
} else {
value = "Obesity";
}
return value;
}
和控制器:
import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.TextField; public class AppController {
@FXML private TextField CMheightInput;
@FXML private TextField INheightInput;
@FXML private TextField KGweightInput;
@FXML private TextField LBSweightInput;
@FXML private Button buttonCalculate;
@FXML private TextField bmiValueOutput;
@FXML private TextField bmiStatusOutput;
BMICalculator calc = new BMICalculator();
BMIInput input = new BMIInput();
@FXML public void calculateBMI() {
try {
input.setHeightCM(Double.parseDouble(CMheightInput.getText()));
input.setHeightIN(Double.parseDouble(INheightInput.getText()));
input.setWeightKG(Double.parseDouble(KGweightInput.getText()));
input.setWeightLBS(Double.parseDouble(LBSweightInput.getText()));
bmiValueOutput.setText("" + calc.calculateBMI());
bmiStatusOutput.setText("" + calc.calculateStatus());
}
catch (Exception e) {
System.out.println("Something went wrong, try again.");
}
}
如果有人有任何指点,我将非常感激! Xx
答案 0 :(得分:0)
您可能没有引用变量的实例...程序启动的Main方法在哪里?您必须在Java文件中加载FXMLController及其资源。尝试观看YouTube视频,了解如何在SceneBuilder中使用简单的JavaFX程序。我喜欢的人之一是:How to use SceneBuilder