【Java开源代码栏目提醒】:网学会员--在 Java开源代码编辑为广大网友搜集整理了:Bean_Constrained.java绩等信息,祝愿广大网友取得需要的信息,参考学习。
import java.beans.*;
public class Bean_Constrained {
private PropertyChangeSupport changes=new PropertyChangeSupport(this);
private VetoableChangeSupport Vetos=new VetoableChangeSupport(this);
/**与前述changes相同,可使用VetoableChangeSupport对象的实例Vetos中的方法,在特定条件下来阻止PriceInCents值的改变。*/
public void setPriceInCents(int newPriceInCents) throws PropertyVetoException {
/**方法名中throws PropertyVetoException的作用是当有其它Java对象否决PriceInCents的改变时,要抛出例外。*/
/**先保存原来的属性值*/
int oldPriceInCents=ourPriceInCents;
/**触发属性改变否决事件*/
vetos.fireVetoableChange("priceInCents",new Integer(OldPriceInCents), new Integer(newPriceInCents));
/**若有其它对象否决priceInCents的改变,则
程序抛出例外,不再继续执行下面的两条语句,方法结束。若无其它对象否决priceInCents的改变,则在下面的代码中把ourPriceIncents赋予新值,并触发属性改变事件*/
ourPriceInCents=newPriceInCents;
changes.firePropertyChange("priceInCents", new Integer(oldPriceInCents),new Integer(newPriceInCents));
}
/**与前述changes相同,也要为PriceInCents属性预留接口,使其它对象可
注册入PriceInCents否决改变监听者队列中,或把该对象从中注销*/
public void addVetoableChangeListener(VetoableChangeListener l){
vetos.addVetoableChangeListener(l);
}
public void removeVetoableChangeListener(VetoableChangeListener l){
vetos.removeVetoableChangeListener(l);
}
}