JavaFx属性绑定

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class App14_8 extends Application {

    @Override
    public void start(Stage primaryStage) {

        // 创建面板对象
        Pane pane = new Pane();
        Circle circle = new Circle();
        // 设置圆中心的x,y坐标为100像素
        circle.setCenterX(100);
        circle.setCenterY(100);

        // 将圆的centerX和centerY属性绑定在面板pane宽和高的一半上
        circle.centerXProperty().bind(pane.widthProperty().divide(2));
        circle.centerYProperty().bind(pane.heightProperty().divide(2));

        // 设置圆半径为50像素
        circle.setRadius(50);
        circle.setFill(Color.RED);
        pane.getChildren().add(circle);

        Scene scene = new Scene(pane, 200, 200);
        primaryStage.setScene(scene);
        primaryStage.setTitle("圆的属性绑定");
        primaryStage.show();

    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}
import javafx.beans.property.SimpleDoubleProperty;


public class App14_9 {

    public static void main(String[] args) {
        // 声明并创建绑定属性t1
        SimpleDoubleProperty t1 = new SimpleDoubleProperty(6);
        // 声明并创建绑定属性t2
        SimpleDoubleProperty t2 = new SimpleDoubleProperty(9);
        // 将t1绑定到t2上
        t1.bind(t2);

        System.out.println("t1值=" + t1.getValue() + ";t2值=" + t2.getValue());

        // 源对象t2变化,目标对象t1的值也会做相应的变化
        t2.setValue(10);
        System.out.println("t1值=" + t1.getValue() + ";t2值=" + t2.getValue());

        t1.unbind();
        // 双向绑定
        t1.bindBidirectional(t2);

        t1.setValue(100);
        System.out.println("t1值=" + t1.getValue() + ";t2值=" + t2.getValue());
        t2.setValue(500);
        System.out.println("t1值=" + t1.getValue() + ";t2值=" + t2.getValue());

    }
}
posted @ 2021-02-15 22:10  xl4ng  阅读(126)  评论(0编辑  收藏  举报