0

任何人都知道如何将一个摇摆 JComponent 绑定到两个 BeansBinding 类(特别是使用 Netbeans IDE)?另外如何将 JFrame 中的变量绑定到 beanbinding 类的属性?

4

1 回答 1

1

A)嗯......仍然不确定你到底想要实现什么:建立一个绑定链,也许?就像是

bean."name" <--> textField."text" --> otherBean.logger

    BindingGroup context = new BindingGroup();
    context.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
            bean, BeanProperty.create("name"), 
            field, BeanProperty.create("text"))); 
    context.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ,
            field, BeanProperty.create("text"), 
            otherBean, BeanProperty.create("logger"))); 

B)Beansbinding 是关于绑定属性而不是字段(又名:变量)。因此,无论您想绑定什么,都需要一个 getter 和(可能是一个 setter,取决于您的要求),并且必须在更改时触发通知。然后像往常一样进行绑定..

public MyFrame extends JFrame {
    private int attempts;

    public int getAttempts() {
        return attempts;
    } 

    private void incrementAttempts() {
        int old = getAttempts();
        attempts++;
        firePropertyChange("attempts", old, getAttempts()); 
    }

    private void bind() {
    BindingGroup context = new BindingGroup();
    context.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ,
            this, BeanProperty.create("attempts"), 
            label, BeanProperty.create("text"))); 

    }
}
于 2011-09-21T16:21:07.047 回答