我正在尝试扩展SimpleDoubleProperty并覆盖set方法,以便在新值和旧值相等时使侦听器代码运行。但是当我运行代码时,它没有触发监听器代码。这里有什么问题?我很难搞清楚这一点:\
SimpleDouble.java
import javafx.beans.property.SimpleDoubleProperty;
public class SimpleDouble extends SimpleDoubleProperty {
public SimpleDouble() {
}
public SimpleDouble(double arg0) {
super(arg0);
}
public SimpleDouble(Object arg0, String arg1) {
super(arg0, arg1);
}
public SimpleDouble(Object arg0, String arg1, double arg2) {
super(arg0, arg1, arg2);
}
@Override
public void set(double arg0) {
if (isBound()) {
throw new java.lang.RuntimeException("A bound value cannot be set.");
}
fireValueChangedEvent();
}
}
Bill.java
class Bill {
private DoubleProperty amountDue = new SimpleDouble();
public final double getAmountDue(){return amountDue.get();}
public final void setAmountDue(double value){amountDue.set(value);}
public DoubleProperty amountDueProperty() {return amountDue;}
}
主要
public class Main {
public static void main(String[] args) {
Bill electricBill = new Bill();
electricBill.amountDueProperty().addListener(new ChangeListener(){
@Override public void changed(ObservableValue o,Object oldVal,
Object newVal){
System.out.println("Electric bill has changed!");
}
});
electricBill.setAmountDue(100.00);
electricBill.setAmountDue(100.00);
}
}