JavaFX属性绑定


属性绑定是javafx新引入的概念,可以将一个目标对象与一个源对象绑定。

1.绑定方法很简单,只有一个简单函数

target.bind(source);
//target相当于因变量,source相当于自变量

2.javafx的许多对象都可以作为自变量或者因变量。

3.绑定类型属性可以当作一个新数据类型。不过它是抽象类不能new出新对象。创新对象需要用下面表格第三列.

类型 绑定属性类型 (抽象类) 创建子类的绑定属性类型
int IntegerProperty SimpleIntegerProperty
long LongProperty SimpleLongProperty
float FloatProperty SimpleFloatProperty
double DoubleProperty SimpleDoubleProperty
boolean BooleanProperty SimpleBooleanProperty
String StringProperty SimpleStringProperty
List ListProperty SimpleListProperty
Set SetProperty SimpleSetProperty
Map MapProperty SimpleMapProperty

4.新属性绑定距离代码:

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;

public class train(){
	public static void main(String[] arge){
		Double a = new SimpleDoubleProperty(6);
		Double b = new SimpleDoubleProperty(9);
		a.bind(b);
		System.out.println("a的值为:"+a+",b的值为:"+b);
		b.setValue(10);
		System.out.println("a的值为:"+a+",b的值为:"+b);
	}
}
//输出结果:a的值为:9,b的值为9
//a的值为10,b的值为10

5.属性绑定代码展示:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;

public class 属性绑定 extends Application {
	@Override
	public void start(Stage qwe) {
		Pane pane = new Pane();
		Circle c = new Circle();
		c.centerXProperty().bind(pane.widthProperty().divide(2));
		c.centerYProperty().bind(pane.heightProperty().divide(2));
		c.setRadius(75);
		c.setFill(Color.BLUE);
		pane.getChildren().add(c);
		Scene scen = new Scene(pane,200,200);
		qwe.setTitle("圆");
		qwe.setScene(scen);
		qwe.show();
	}
	public static void main(String[] args) {
		Application.launch(args);
	}

}
>运行结果:产生一个圆,不论界面如何变化,圆始终位于窗口中间。

6.还可以双向绑定:bindDidirectional().


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM