gpt4 book ai didi

binding - JavaFX 将 BooleanProperty 绑定(bind)到 Paint

转载 作者:行者123 更新时间:2023-12-04 00:29:04 24 4
gpt4 key购买 nike

我正在编写一个代码,它可以从其他代码的一部分中获取一些 bool 值,并相应地改变屏幕上某些圆圈的颜色。但是,我在尝试将 bool 值绑定(bind)到颜色时遇到了问题。我最终得到了这个:

unit1.getNeuron().getWorkingProperty().addListener(new ChangeListener<Boolean>() {

@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (newValue == Boolean.FALSE) {
controller.paint1 = new ObservableValueBase<Paint>() {

@Override
public Paint getValue() {
return Color.RED;
}
};
} else {
controller.paint1 = new ObservableValueBase<Paint>() {

@Override
public Paint getValue() {
return Color.DODGERBLUE;
}
};

}
}
});
  • 但我必须为我使用的 n 个变量重复 n 次。有没有不同的方法来实现它?

最佳答案

假设您要创建一个 ObservableObjectValue<Paint>你想根据 ObservableBooleanValue 切换, 然后 Bindings 是你的 friend :

final ObservableBooleanValue booleanCondition = unit1.getNeuron().getWorkingProperty();
final ObservableObjectValue<Paint> paintProperty = Bindings.when(booleanCondition).then(Color.RED).otherwise(Color.DODGERBLUE);

关于binding - JavaFX 将 BooleanProperty 绑定(bind)到 Paint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29672418/

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