我有一个ChangeListener
,当触发时,会调用findGPA()
方法。
private void findGPA(){
GPA = gradeCre/sum;
decGPA = new BigDecimal(GPA);
decGPA = decGPA.setScale(2, RoundingMode.CEILING);
System.out.println("Your average GPA is: " + decGPA);
}
BigDecimal
初始化如下:
private BigDecimal decGPA = BigDecimal.ZERO;
在initialize()
方法中:
SubmitStageBorderPane.setBottom(AddStackCircle());
调用:
private StackPane AddStackCircle(){
StackPane stackCircle = new StackPane();
StringProperty gpa = new SimpleStringProperty("");
gpa.bind(new SimpleStringProperty(decGPA.toString()));
Text avgGPA = createText("Your semester GPA is: " + gpa);
Circle resultCircle = createCircle(avgGPA);
stackCircle.getStyleClass().add("stackCircle");
stackCircle.getChildren().addAll(resultCircle, avgGPA);
return stackCircle;
}
private Circle createCircle(Text avgGPA){
Circle resultCircle = new Circle();
resultCircle.setFill(Color.GREEN);
resultCircle.setStroke(Color.GREY);
resultCircle.setStrokeWidth(3);
resultCircle.setRadius(getWidth(avgGPA) / 2 + 10);
return resultCircle;
}
private Text createText(String text){
Text avgGPA = new Text(text);
avgGPA.setBoundsType(TextBoundsType.VISUAL);
avgGPA.getStyleClass().add("avgGPA");
return avgGPA;
}
但是,当我运行它时,它会生成以下标签文本Your semester GPA is: StringProperty[bound, invalid]
,即使decGPA
的值发生更改,它也不会更改。
答案 0 :(得分:2)
要更新文本,您需要绑定Text
节点的文本,而不是简单地设置文本。此外,您应该将它绑定到实际更新的属性,而不仅仅是一个永远不会被修改的属性(如new SimpleStringProperty(decGPA.toString())
)。
假设正确调用了findGPA
方法,它应该像这样实现:
private final ObjectProperty<BigDecimal> decGPA = new SimpleObjectProperty(BigDecimal.ZERO);
private void findGPA(){
GPA = gradeCre/sum;
decGPA.set(BigDecimal.valueOf(GPA).setScale(2, RoundingMode.CEILING));
System.out.println("Your average GPA is: " + decGPA.get());
}
private Text createText(ObservableValue<String> textExpression){
Text avgGPA = new Text();
avgGPA.textProperty().bind(textExpression);
avgGPA.setBoundsType(TextBoundsType.VISUAL);
avgGPA.getStyleClass().add("avgGPA");
return avgGPA;
}
Text avgGPA = createText(decGPA.asString("Your semester GPA is: %s"));