Data binding java8绑定两个浮点数
我所要做的就是将一个浮点数绑定到另一个浮点数:Data binding java8绑定两个浮点数,data-binding,javafx,java-8,Data Binding,Javafx,Java 8,我所要做的就是将一个浮点数绑定到另一个浮点数: import javafx.beans.property.FloatProperty; import javafx.beans.property.SimpleFloatProperty; public class BindingTest { public static void main(String[] args) { float bound=0; float binder= -1; F
import javafx.beans.property.FloatProperty;
import javafx.beans.property.SimpleFloatProperty;
public class BindingTest
{
public static void main(String[] args)
{
float bound=0;
float binder= -1;
FloatProperty boundP= new SimpleFloatProperty(bound);
FloatProperty binderP=new SimpleFloatProperty(binder);
binderP.bind(boundP);
System.out.println("Before: \n\tbinder: " +binder + "\n\tbound: " + bound);
binder= 23;
System.out.println("After: \n\tbinder: " +binder + "\n\tbound: " + bound);
}
}
如果您费心运行此操作,则当变量活页夹
更改为值23时,变量绑定
将不会更新。
我做错了什么?
谢谢 所以我认为你对属性如何工作的想法是错误的。为了更好地理解,我举了一个例子:
import javafx.beans.binding.Bindings;
import javafx.beans.binding.NumberBinding;
import javafx.beans.property.FloatProperty;
import javafx.beans.property.SimpleFloatProperty;
public class Test {
public static void main(String... args) {
FloatProperty dummyProp = new SimpleFloatProperty(0);
FloatProperty binderP = new SimpleFloatProperty(-1);
//Means boundP = dummyProp + binderP
NumberBinding boundP = Bindings.add(binderP, dummyProp);
System.out.format("%f + %f = %f%n", dummyProp.floatValue(), binderP.floatValue(), boundP.floatValue());
dummyProp.set(2);
System.out.format("%f + %f = %f%n", dummyProp.floatValue(), binderP.floatValue(), boundP.floatValue());
// dummyProp is equal to binderP but dummyProp is a read-only value
dummyProp.bind(binderP);
System.out.format("%f, %f%n", dummyProp.floatValue(), binderP.floatValue());
// throws an exception because dummyProp is bound to binderP
// dummyProp.set(5);
binderP.set(5f);
System.out.format("%f, %f%n", dummyProp.floatValue(), binderP.floatValue());
dummyProp.unbind();
// dummyProp == binderP always
dummyProp.bindBidirectional(binderP);
System.out.format("%f, %f%n", dummyProp.floatValue(), binderP.floatValue());
dummyProp.set(3f);
System.out.format("%f, %f%n", dummyProp.floatValue(), binderP.floatValue());
binderP.set(10f);
System.out.format("%f, %f%n", dummyProp.floatValue(), binderP.floatValue());
// boundP always consists of the sum
System.out.format("%f%n", boundP.floatValue());
}
}
我认为您需要再次阅读教程来了解绑定是如何工作的:Thank flwn--在下面的回答中发布了回复您知道值调用是什么意思吗?谢谢,明白了。非常感谢!