2013-01-14 54 views
0

我试图将我的打印方法绑定到xProperty()(print().bind(scene.xProperty());)。它运行一次,但是当xProperty被调用时,我的方法不会再被调用。我怎样才能打电话多次。绑定编译但未被调用

public DoubleProperty print(){ 
    System.out.println("print"); 
    DoubleProperty dp = new DoubleProperty(){ 
     public void removeListener(ChangeListener cl){} 
     public void removeListener(InvalidationListener cl){} 
     public void addListener(ChangeListener cl){} 
     public void addListener(InvalidationListener cl){} 
     public double get(){return 10;} 
     public String getName(){return "";} 
     public Object getBean(){return new Object();} 
     public boolean isBound(){return true;} 
     public void unbind(){} 
     public void bind(ObservableValue observable){} 
     public void set(double d){} 
    }; 
    return dp; 

} 
+0

你的目标是什么?你想调用一个有史以来场景x值改变的方法吗? – invariant

+0

正确。我想出了如何用变化监听器来做到这一点。我想用bind来代替。 \t public chart(Vector v,Scene s){ \t \t super(); \t \t this.v = v; \t \t s.widthProperty()。的addListener(新的ChangeListener (){ \t \t \t公共无效改变(ObservableValue <?延伸号码> OV, \t \t \t号old_val,编号new_val){ \t \t \t \t平局(); \t \t \t} \t \t}); \t} – user1958884

回答

0

我不知道你在这里寻找什么,但不是从头开始实现自己的DoubleProperty(你的实现是缺少听众的正确处理,例如)我会建议使用SimpleDoubleProperty作为基础。

例如:

public class Bean { 
    private DoubleProperty print; 
    public DoubleProperty printProperty(){ 
     if (print == null) 
      print = new SimpleDoubleProperty(this, "print"); 
     return print; 
    } 
} 

现在,您可以绑定到打印性能并在其上注册自己的听众也是如此。

+0

感谢您的回复,但我不知道如何实现它,所以它只会调用一个方法。我发布的示例可能是错误的。我希望能够调用从Canvas中绘制GraphicsContext2D所用的所有方法,当它调整父级场景的大小时。 – user1958884