gpt4 book ai didi

java - 如何将 StringProperty 绑定(bind)到 BigDecimal?

转载 作者:行者123 更新时间:2023-12-02 02:40:32 24 4
gpt4 key购买 nike

我有一个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 的值也不会改变。变化。

最佳答案

要更新文本,您需要绑定(bind) Text 节点的文本,而不是简单地设置文本。此外,您应该将其绑定(bind)到实际更新的属性,而不仅仅是永远不会修改的属性(例如 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"));

关于java - 如何将 StringProperty 绑定(bind)到 BigDecimal?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45545500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com