这就是我的代码的样子:
Class A {
private boolean valueChanged;
public static void main(String args[]) {
B obj = new B();
obj.addPropertyChangeListener("valueChanged", new ValueChangeListener());
obj.someMethodThatFiresChange();
}
private class ValueChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent evt) {
if(Boolean.TRUE.equals(evt.getNewValue()) {
doSomething(); //code never breaks here!!
}
}
}
}
Class B {
private boolean valueChanged;
public void setValueChanged(boolean b) {
boolean oldVal = valueChanged;
valueChanged = b;
firePropertyChange("valueChanged", oldVal, valueChanged);
}
public void someMethodThatFiresChange() {
setValueChanged(true);
}
}
然而,这段代码似乎不起作用——它永远不会到达事件触发时应该执行的块!我在这里做错了什么?