试试吧...
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
public class CyclicPropertyTest {
public static void main(String[] args) {
IntegerProperty x = new SimpleIntegerProperty(0);
IntegerProperty y = new SimpleIntegerProperty(1);
IntegerProperty z = new SimpleIntegerProperty(2);
x.addListener((obs, oldValue, newValue) -> System.out.printf("x changed from %d to %d %n", oldValue, newValue));
y.addListener((obs, oldValue, newValue) -> System.out.printf("y changed from %d to %d %n", oldValue, newValue));
z.addListener((obs, oldValue, newValue) -> System.out.printf("z changed from %d to %d %n", oldValue, newValue));
x.bindBidirectional(y);
y.bindBidirectional(z);
z.bindBidirectional(x);
x.set(1);
}
}
Run Code Online (Sandbox Code Playgroud)
仅在属性值更改时才通知侦听器.当x设置为1时,这会导致y设置为1,这会导致z设置为1.这会激活侦听器.由于z已经改变,这导致(至少在概念上)x被设置为1,但由于它已经是1,所以没有通知监听器,因此循环终止.