Javafx 侦听器正在工作,但未绑定

Javafx 侦听器正在工作,但未绑定,javafx,Javafx,下面是一个简单的工作示例: public class FF { @Test public void test01() { final ListProperty p = new SimpleListProperty(FXCollections.observableArrayList()); p.addListener((ListChangeListener) c -> { System.err.println("Listener here..

下面是一个简单的工作示例:

public class FF {

    @Test
    public void test01() {
    final ListProperty p = new SimpleListProperty(FXCollections.observableArrayList());
    p.addListener((ListChangeListener) c -> {
        System.err.println("Listener here..");
    });
    Bindings.createObjectBinding(() -> {
        System.err.println("Binding here");
        return null;
    }, p);

    p.add("hans");
    }

    @Test
    public void test02() {
    final ListProperty p = new SimpleListProperty(FXCollections.observableArrayList());
    final ListProperty p2 = new SimpleListProperty(FXCollections.observableArrayList());
    p.addListener((ListChangeListener) c -> {
        System.err.println("Listener 1 here..");
    });
    p2.addListener((ListChangeListener) c -> {
        System.err.println("Listener 2 here..");
    });
    final ObjectBinding ob = Bindings.createObjectBinding(() -> {
        System.err.println("Binding here");
        return null;
    }, p);
    p2.bind(ob);
    p.add("hans");

    }

}
第二个测试看起来像预期的,但对于第一个测试,输出只是“Listener here…”。为什么在这种情况下绑定不起作用


匿名侦听器和匿名绑定之间的区别是什么?

您创建了绑定对象,但没有通过它绑定任何内容,因此它优化了自身,使其不做任何事情

添加任意随机操作以查看“binding here…”输出。例如:

    ObjectBinding<ObservableList> ob = Bindings.createObjectBinding(() -> {
        System.err.println("Binding here");
        return null;
    }, p);
    ob.addListener((o) -> {
        System.out.println("random action");
    });
ObjectBinding ob=Bindings.createObjectBinding(()->{
System.err.println(“此处绑定”);
返回null;
},p);
ob.addListener((o)->{
System.out.println(“随机动作”);
});