JavaFX(五)之属性绑定和属性监听

属性绑定:

就是将目标对象的数值或某项数值和另一个源对象的数值进行关联,让二者存在某关系,当源对象发生改变时,根据两者的关联关系,目标对象的数值发生相应的变化。

属性监听:

对目标对象的数值的变化进行监测。
属性绑定和属性监听在JavaFX中都可以通过接口Property来实现。
继承Property接口的类有很多:有BooleanProperty,intProperty,doubleProperty等基本数据类型的,也有ListProperty,MapProperty<K,V>等。
绑定用的是bind()方法,这是单向绑定。还有双向绑定和解除绑定等。
监听是Listener(),其中也会关联到change。

package javafx;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class Main5 extends Application {
    
    
    public static void main(String[] args) {
    
    
        Application.launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
    
    
        AnchorPane root = new AnchorPane();
        Scene scene = new Scene(root,360,360);
        Circle circle = new Circle(); //Circle是圆
        circle.setCenterX(123);
        circle.setCenterY(123);
        circle.setRadius(88); //Radius为半径
        circle.setStroke(Color.YELLOW);
        circle.setFill(Color.BLUE);

        circle.centerXProperty().bind(scene.widthProperty().divide(2));
        circle.centerYProperty().bind(scene.heightProperty().divide(2));
        //bind为单向绑定,只有circle随scene变化,scene不会因为circle的变化而变化
        //圆的中心绑定了场景的宽高,会随之变化
        //以上为属性绑定的测试

        circle.centerXProperty().addListener(new ChangeListener<Number>() {
    
    
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
    
    
                System.out.println("x轴原来是:"+ oldValue + "现在是:" + newValue);
            }
        });
        circle.centerYProperty().addListener(new ChangeListener<Number>() {
    
    
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
    
    
                System.out.println("y轴原来是:"+ oldValue + "现在是:" + newValue);
            }
        });
        //以上为属性绑定,设置之后圆的变化就会被监听,用了输出语句可以表示出来
        root.getChildren().add(circle);


        primaryStage.setScene(scene);
        primaryStage.show();

    }
}

运行测试:
在这里插入图片描述
变化一下场景大小:
在这里插入图片描述

x轴原来是:180.0现在是:180.3333282470703
x轴原来是:180.3333282470703现在是:181.0
x轴原来是:181.0现在是:182.0
y轴原来是:180.0现在是:180.6666717529297
x轴原来是:182.0现在是:182.6666717529297
y轴原来是:180.6666717529297现在是:181.6666717529297
y轴原来是:181.6666717529297现在是:182.3333282470703
x轴原来是:182.6666717529297现在是:183.0
y轴原来是:182.3333282470703现在是:182.6666717529297
x轴原来是:183.0现在是:183.3333282470703
y轴原来是:182.6666717529297现在是:183.3333282470703
x轴原来是:183.3333282470703现在是:183.6666717529297
x轴原来是:183.6666717529297现在是:184.0
y轴原来是:183.3333282470703现在是:184.0
x轴原来是:184.0现在是:184.3333282470703
y轴原来是:184.0现在是:184.6666717529297
x轴原来是:184.3333282470703现在是:185.6666717529297
y轴原来是:184.6666717529297现在是:186.6666717529297
x轴原来是:185.6666717529297现在是:186.0
y轴原来是:186.6666717529297现在是:187.3333282470703
x轴原来是:186.0现在是:186.3333282470703
y轴原来是:187.3333282470703现在是:188.0
x轴原来是:186.3333282470703现在是:187.0
y轴原来是:188.0现在是:189.0
x轴原来是:187.0现在是:188.0
y轴原来是:189.0现在是:190.3333282470703
x轴原来是:188.0现在是:189.3333282470703
y轴原来是:190.3333282470703现在是:191.0
x轴原来是:189.3333282470703现在是:190.6666717529297
y轴原来是:191.0现在是:193.0
x轴原来是:190.6666717529297现在是:191.3333282470703
y轴原来是:193.0现在是:194.0
x轴原来是:191.3333282470703现在是:192.3333282470703
y轴原来是:194.0现在是:195.3333282470703
x轴原来是:192.3333282470703现在是:193.6666717529297
y轴原来是:195.3333282470703现在是:196.6666717529297
x轴原来是:193.6666717529297现在是:194.3333282470703
y轴原来是:196.6666717529297现在是:197.3333282470703
x轴原来是:194.3333282470703现在是:195.3333282470703
y轴原来是:197.3333282470703现在是:198.6666717529297
x轴原来是:195.3333282470703现在是:196.0
y轴原来是:198.6666717529297现在是:199.3333282470703
x轴原来是:196.0现在是:196.6666717529297
y轴原来是:199.3333282470703现在是:199.6666717529297
x轴原来是:196.6666717529297现在是:198.0
y轴原来是:199.6666717529297现在是:200.6666717529297
x轴原来是:198.0现在是:199.0
y轴原来是:200.6666717529297现在是:201.3333282470703
x轴原来是:199.0现在是:199.3333282470703
y轴原来是:201.3333282470703现在是:202.0
x轴原来是:199.3333282470703现在是:199.6666717529297
y轴原来是:202.0现在是:203.0
x轴原来是:199.6666717529297现在是:200.3333282470703
y轴原来是:203.0现在是:204.0
x轴原来是:200.3333282470703现在是:201.0
y轴原来是:204.0现在是:204.3333282470703
x轴原来是:201.0现在是:201.6666717529297
y轴原来是:204.3333282470703现在是:204.6666717529297
x轴原来是:201.6666717529297现在是:202.0
y轴原来是:204.6666717529297现在是:205.3333282470703
x轴原来是:202.0现在是:202.3333282470703
y轴原来是:205.3333282470703现在是:206.0
x轴原来是:202.3333282470703现在是:203.0
y轴原来是:206.0现在是:206.3333282470703
x轴原来是:203.0现在是:203.3333282470703
y轴原来是:206.3333282470703现在是:207.0
x轴原来是:203.3333282470703现在是:204.3333282470703
y轴原来是:207.0现在是:207.3333282470703
x轴原来是:204.3333282470703现在是:205.0
y轴原来是:207.3333282470703现在是:208.3333282470703
x轴原来是:205.0现在是:205.6666717529297
y轴原来是:208.3333282470703现在是:208.6666717529297
x轴原来是:205.6666717529297现在是:206.0
y轴原来是:208.6666717529297现在是:209.3333282470703
x轴原来是:206.0现在是:207.3333282470703
y轴原来是:209.3333282470703现在是:210.3333282470703
x轴原来是:207.3333282470703现在是:208.0
y轴原来是:210.3333282470703现在是:211.0
x轴原来是:208.0现在是:208.3333282470703
y轴原来是:211.0现在是:211.3333282470703
x轴原来是:208.3333282470703现在是:209.3333282470703
y轴原来是:211.3333282470703现在是:212.0
x轴原来是:209.3333282470703现在是:209.6666717529297
y轴原来是:212.0现在是:212.3333282470703
x轴原来是:209.6666717529297现在是:210.0
y轴原来是:212.3333282470703现在是:212.6666717529297
x轴原来是:210.0现在是:210.6666717529297
y轴原来是:212.6666717529297现在是:213.3333282470703
x轴原来是:210.6666717529297现在是:211.0
y轴原来是:213.3333282470703现在是:214.0
x轴原来是:211.0现在是:211.3333282470703
y轴原来是:214.0现在是:214.3333282470703
y轴原来是:214.3333282470703现在是:214.6666717529297
x轴原来是:211.3333282470703现在是:212.0
y轴原来是:214.6666717529297现在是:215.3333282470703
y轴原来是:215.3333282470703现在是:216.0
x轴原来是:212.0现在是:212.3333282470703
x轴原来是:212.3333282470703现在是:212.6666717529297
x轴原来是:212.6666717529297现在是:213.0
y轴原来是:216.0现在是:216.3333282470703
y轴原来是:216.3333282470703现在是:216.6666717529297
y轴原来是:216.6666717529297现在是:217.0
x轴原来是:213.0现在是:213.3333282470703
y轴原来是:217.0现在是:217.3333282470703
y轴原来是:217.3333282470703现在是:217.6666717529297
x轴原来是:213.3333282470703现在是:213.6666717529297
y轴原来是:217.6666717529297现在是:218.0
x轴原来是:213.6666717529297现在是:214.0
x轴原来是:214.0现在是:214.3333282470703
y轴原来是:218.0现在是:218.3333282470703
x轴原来是:214.3333282470703现在是:214.6666717529297
y轴原来是:218.3333282470703现在是:218.6666717529297
y轴原来是:218.6666717529297现在是:219.0
x轴原来是:214.6666717529297现在是:215.3333282470703
y轴原来是:219.0现在是:219.3333282470703
x轴原来是:215.3333282470703现在是:215.6666717529297
x轴原来是:215.6666717529297现在是:216.0
y轴原来是:219.3333282470703现在是:220.0
x轴原来是:216.0现在是:216.3333282470703
y轴原来是:220.0现在是:220.3333282470703
y轴原来是:220.3333282470703现在是:220.6666717529297
y轴原来是:220.6666717529297现在是:221.0
x轴原来是:216.3333282470703现在是:216.6666717529297
y轴原来是:221.0现在是:221.3333282470703
x轴原来是:216.6666717529297现在是:217.0
y轴原来是:221.3333282470703现在是:221.6666717529297
y轴原来是:221.6666717529297现在是:222.0
x轴原来是:217.0现在是:217.3333282470703
y轴原来是:222.0现在是:222.3333282470703
y轴原来是:222.3333282470703现在是:222.6666717529297
y轴原来是:222.6666717529297现在是:223.0
y轴原来是:223.0现在是:223.3333282470703
x轴原来是:217.3333282470703现在是:217.6666717529297
x轴原来是:217.6666717529297现在是:218.0
y轴原来是:223.3333282470703现在是:223.6666717529297
y轴原来是:223.6666717529297现在是:224.0
y轴原来是:224.0现在是:224.3333282470703
y轴原来是:224.3333282470703现在是:224.6666717529297
x轴原来是:218.0现在是:218.6666717529297
y轴原来是:224.6666717529297现在是:225.0
x轴原来是:218.6666717529297现在是:219.0
y轴原来是:225.0现在是:225.6666717529297
y轴原来是:225.6666717529297现在是:226.0
x轴原来是:219.0现在是:219.3333282470703
y轴原来是:226.0现在是:226.3333282470703
y轴原来是:226.3333282470703现在是:226.6666717529297
y轴原来是:226.6666717529297现在是:227.0
x轴原来是:219.3333282470703现在是:219.6666717529297
x轴原来是:219.6666717529297现在是:220.3333282470703
y轴原来是:227.0现在是:228.0
x轴原来是:220.3333282470703现在是:220.6666717529297
y轴原来是:228.0现在是:228.3333282470703
x轴原来是:220.6666717529297现在是:221.0
y轴原来是:228.3333282470703现在是:228.6666717529297
x轴原来是:221.0现在是:221.3333282470703
y轴原来是:228.6666717529297现在是:229.0
x轴原来是:221.3333282470703现在是:222.0
y轴原来是:229.0现在是:230.0
x轴原来是:222.0现在是:223.0
y轴原来是:230.0现在是:231.0
x轴原来是:223.0现在是:223.6666717529297
y轴原来是:231.0现在是:232.0
x轴原来是:223.6666717529297现在是:224.3333282470703
y轴原来是:232.0现在是:233.0
x轴原来是:224.3333282470703现在是:224.6666717529297
y轴原来是:233.0现在是:233.3333282470703
x轴原来是:224.6666717529297现在是:225.3333282470703
y轴原来是:233.3333282470703现在是:233.6666717529297
x轴原来是:225.3333282470703现在是:225.6666717529297
y轴原来是:233.6666717529297现在是:234.6666717529297
x轴原来是:225.6666717529297现在是:226.0
y轴原来是:234.6666717529297现在是:235.3333282470703
x轴原来是:226.0现在是:226.3333282470703
y轴原来是:235.3333282470703现在是:235.6666717529297
y轴原来是:235.6666717529297现在是:236.0
x轴原来是:226.3333282470703现在是:227.3333282470703
y轴原来是:236.0现在是:237.3333282470703
x轴原来是:227.3333282470703现在是:228.3333282470703
y轴原来是:237.3333282470703现在是:238.6666717529297
x轴原来是:228.3333282470703现在是:229.3333282470703
y轴原来是:238.6666717529297现在是:240.0
x轴原来是:229.3333282470703现在是:229.6666717529297
y轴原来是:240.0现在是:240.3333282470703
x轴原来是:229.6666717529297现在是:230.0
x轴原来是:230.0现在是:230.3333282470703
y轴原来是:240.3333282470703现在是:240.6666717529297
y轴原来是:240.6666717529297现在是:241.0
x轴原来是:230.3333282470703现在是:230.6666717529297
y轴原来是:241.0现在是:241.3333282470703
y轴原来是:241.3333282470703现在是:241.6666717529297
x轴原来是:230.6666717529297现在是:231.0
x轴原来是:231.0现在是:231.3333282470703
x轴原来是:231.3333282470703现在是:231.6666717529297
y轴原来是:241.6666717529297现在是:242.0
y轴原来是:242.0现在是:242.3333282470703
x轴原来是:231.6666717529297现在是:232.0
y轴原来是:242.3333282470703现在是:242.6666717529297
x轴原来是:232.0现在是:232.3333282470703
y轴原来是:242.6666717529297现在是:243.0
x轴原来是:232.3333282470703现在是:233.0
y轴原来是:243.0现在是:243.6666717529297
x轴原来是:233.0现在是:233.3333282470703
y轴原来是:243.6666717529297现在是:244.0
y轴原来是:244.0现在是:244.3333282470703
x轴原来是:233.3333282470703现在是:233.6666717529297
y轴原来是:244.3333282470703现在是:244.6666717529297
x轴原来是:233.6666717529297现在是:234.0
y轴原来是:244.6666717529297现在是:245.0
x轴原来是:234.0现在是:234.3333282470703
x轴原来是:234.3333282470703现在是:234.6666717529297
y轴原来是:245.0现在是:245.3333282470703
x轴原来是:234.6666717529297现在是:235.0
y轴原来是:245.3333282470703现在是:245.6666717529297
y轴原来是:245.6666717529297现在是:246.0
x轴原来是:235.0现在是:235.3333282470703
y轴原来是:246.0现在是:246.3333282470703
x轴原来是:235.3333282470703现在是:235.6666717529297
x轴原来是:235.6666717529297现在是:236.0
x轴原来是:236.0现在是:236.6666717529297
x轴原来是:236.6666717529297现在是:237.3333282470703
x轴原来是:237.3333282470703现在是:237.6666717529297
x轴原来是:237.6666717529297现在是:238.0
x轴原来是:238.0现在是:238.3333282470703
x轴原来是:238.3333282470703现在是:238.6666717529297
x轴原来是:238.6666717529297现在是:239.3333282470703
x轴原来是:239.3333282470703现在是:240.3333282470703
x轴原来是:240.3333282470703现在是:240.6666717529297
x轴原来是:240.6666717529297现在是:241.3333282470703
x轴原来是:241.3333282470703现在是:243.0
x轴原来是:243.0现在是:245.0
x轴原来是:245.0现在是:247.6666717529297
x轴原来是:247.6666717529297现在是:249.6666717529297
x轴原来是:249.6666717529297现在是:251.6666717529297
x轴原来是:251.6666717529297现在是:256.3333435058594
x轴原来是:256.3333435058594现在是:258.6666564941406
x轴原来是:258.6666564941406现在是:261.3333435058594
x轴原来是:261.3333435058594现在是:265.3333435058594
x轴原来是:265.3333435058594现在是:267.3333435058594
x轴原来是:267.3333435058594现在是:271.6666564941406
x轴原来是:271.6666564941406现在是:273.6666564941406
x轴原来是:273.6666564941406现在是:278.0
x轴原来是:278.0现在是:279.6666564941406
x轴原来是:279.6666564941406现在是:285.0
x轴原来是:285.0现在是:286.6666564941406
x轴原来是:286.6666564941406现在是:287.0
x轴原来是:287.0现在是:287.6666564941406
x轴原来是:287.6666564941406现在是:288.3333435058594
x轴原来是:288.3333435058594现在是:289.6666564941406
x轴原来是:289.6666564941406现在是:290.3333435058594
x轴原来是:290.3333435058594现在是:291.0
x轴原来是:291.0现在是:292.0
x轴原来是:292.0现在是:295.3333435058594
x轴原来是:295.3333435058594现在是:296.3333435058594
x轴原来是:296.3333435058594现在是:300.0
x轴原来是:300.0现在是:302.3333435058594
x轴原来是:302.3333435058594现在是:303.6666564941406
x轴原来是:303.6666564941406现在是:305.6666564941406
x轴原来是:305.6666564941406现在是:306.6666564941406
x轴原来是:306.6666564941406现在是:307.6666564941406
x轴原来是:307.6666564941406现在是:309.0
x轴原来是:309.0现在是:309.6666564941406
x轴原来是:309.6666564941406现在是:313.0
x轴原来是:313.0现在是:314.6666564941406
x轴原来是:314.6666564941406现在是:316.6666564941406
x轴原来是:316.6666564941406现在是:317.3333435058594
x轴原来是:317.3333435058594现在是:319.3333435058594
x轴原来是:319.3333435058594现在是:320.0
x轴原来是:320.0现在是:320.6666564941406
x轴原来是:320.6666564941406现在是:321.0
x轴原来是:321.0现在是:321.3333435058594
x轴原来是:321.3333435058594现在是:321.6666564941406
x轴原来是:321.6666564941406现在是:322.0
x轴原来是:322.0现在是:322.3333435058594
x轴原来是:322.3333435058594现在是:323.0

猜你喜欢

转载自blog.csdn.net/YiNianShangE/article/details/125127418