gpt4 book ai didi

java - 如何绑定(bind)到 JavaFX 中的常量?

转载 作者:行者123 更新时间:2023-11-30 10:37:48 25 4
gpt4 key购买 nike

如何在 JavaFX 中绑定(bind)常量?

我找到了一个方法

myObject.myIntegerProperty().bind(new IntegerBinding() {
@Override
protected int computeValue() {
return 2;
}
});

但是看起来太过分了。

有没有短路的方法?

最佳答案

作为James_D pointed out :无需创建常量绑定(bind),只需使用给定值初始化属性即可。但是,我建议使用 ReadOnlyIntegerProperty确保该属性未在其他地方被修改:

public final class MyClass {

private final ReadOnlyIntegerWrapper myInteger = new ReadOnlyIntegerWrapper(2);

public int getMyInteger() {
return myInteger.get();
}

public ReadOnlyIntegerProperty myIntegerProperty() {
return myInteger.getReadOnlyProperty();
}

}

注意类应该是final according to Jonathan Giles :

Jeff Frieson has an article about read-only properties in JavaFX. The article lacks a little clarity, so I was a little reticent to include it. The biggest issue is the fact that the methods are not final (although the class is, but I worry people won’t notice this). The methods not being final provides a huge issue if subclasses try to override the getter / setter methods and include logic (because then there are two code paths – setting via the setter and setting via the property method – and they do not overlap, resulting in unexpected behaviour at runtime. The standard rule applies – when creating getter / setter / property methods, always make them final.

关于java - 如何绑定(bind)到 JavaFX 中的常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40005107/

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