Property Binding
上一节
下一节
Property Binding
DoubleProperty target = new SimpleDoubleProperty(1.0);
DoubleProperty source = new SimpleDoubleProperty(2.0);
target.bind(source); //target is 1.0, source is 2.0
source.setValue(88.88); //target is 88.88, source is 88.88
JavaFX提供了可以绑定的属性对象,例如DoubleProperty、IntegerProperty等,目标对象通过bind方法绑定原对象,当原对象变化时,目标对象也随之变化。
Pane pane = new Pane();
Circle circle = new Circle();
circle.centerXProperty().bind(pane.widthProperty().divide(2));
circle.centerYProperty().bind(pane.heightProperty().divide(2));
JavaFX的节点类中的设置了一些可绑定的属性类型,例如Circle类中圆心的x坐标和y坐标,Pane类中的长和宽等,通过两者属性的绑定可以实现一些特定效果,比如随着窗口变化,圆的位置一直保持在中心。

