Java 如何将2个文本字段和至少3个复选框中的1个绑定到按钮
现在,我有这个按钮,我想启用一旦2个文本字段已填写,并至少有三分之一的复选框被选中。到目前为止,这就是我所拥有的,但它似乎没有按照预期的方式工作。我将非常感谢你的任何意见。先谢谢你Java 如何将2个文本字段和至少3个复选框中的1个绑定到按钮,java,javafx,binding,Java,Javafx,Binding,现在,我有这个按钮,我想启用一旦2个文本字段已填写,并至少有三分之一的复选框被选中。到目前为止,这就是我所拥有的,但它似乎没有按照预期的方式工作。我将非常感谢你的任何意见。先谢谢你 button.disableProperty().bind( Bindings.isEmpty(textField1.textProperty()) .or(Bindings.isEmpty(textField2.textProperty())) .or(checkBox1.selectedProp
button.disableProperty().bind(
Bindings.isEmpty(textField1.textProperty())
.or(Bindings.isEmpty(textField2.textProperty()))
.or(checkBox1.selectedProperty())
.or(checkBox2.selectedProperty())
.or(checkBox3.selectedProperty())
);
好的,我实际上尝试了下面列出的解决方案。它需要填写2个文本字段,并在3个复选框中至少选中1个,这似乎是可行的
button.disableProperty().bind(
Bindings.isEmpty(textField1.textProperty())
.and(Bindings.isEmpty(textField2.textProperty()))
.or(checkBox1.selectedProperty().not())
.and(checkBox2.selectedProperty().not())
.and(checkBox3.selectedProperty().not())
);
好的,我实际上尝试了下面列出的解决方案。它需要填写2个文本字段,并在3个复选框中至少选中1个,这似乎是可行的
button.disableProperty().bind(
Bindings.isEmpty(textField1.textProperty())
.and(Bindings.isEmpty(textField2.textProperty()))
.or(checkBox1.selectedProperty().not())
.and(checkBox2.selectedProperty().not())
.and(checkBox3.selectedProperty().not())
);
我不太喜欢用链接
或
,和
和非
来创建表达式,原因很简单。实际表达式不容易理解<代码>绑定。createBooleanBinding生成的代码更易于阅读:
button.disableProperty().bind(Bindings.createBooleanBinding(
() -> {
if (textField1.getText().isEmpty() || textField2.getText().isEmpty()) {
return true;
}
return !(checkBox1.isSelected() || checkBox2.isSelected() || checkBox3.isSelected());
},
textField1.textProperty(), textField1.textProperty(), checkBox1.selectedProperty(), checkBox2.selectedProperty(), checkBox3.selectedProperty()
));
如果坚持链接和,或和而不是,则应创建以下表达式:
(e1 || e2 || !(s1 || s2 || s3))
其中e1
和e2
代表空文本字段,而s1
,s2
,s3
代表所选复选框
button.disableProperty().bind(
textField1.textProperty().isEmpty()
.or(textField2.textProperty().isEmpty())
.or(
checkBox1.selectedProperty()
.or(checkBox2.selectedProperty())
.or(checkBox3.selectedProperty()).not())
);
我不太喜欢用链接或
,和
和非
来创建表达式,原因很简单。实际表达式不容易理解<代码>绑定。createBooleanBinding
生成的代码更易于阅读:
button.disableProperty().bind(Bindings.createBooleanBinding(
() -> {
if (textField1.getText().isEmpty() || textField2.getText().isEmpty()) {
return true;
}
return !(checkBox1.isSelected() || checkBox2.isSelected() || checkBox3.isSelected());
},
textField1.textProperty(), textField1.textProperty(), checkBox1.selectedProperty(), checkBox2.selectedProperty(), checkBox3.selectedProperty()
));
如果坚持链接和,或和而不是,则应创建以下表达式:
(e1 || e2 || !(s1 || s2 || s3))
其中e1
和e2
代表空文本字段,而s1
,s2
,s3
代表所选复选框
button.disableProperty().bind(
textField1.textProperty().isEmpty()
.or(textField2.textProperty().isEmpty())
.or(
checkBox1.selectedProperty()
.or(checkBox2.selectedProperty())
.or(checkBox3.selectedProperty()).not())
);
让e1
和e2
表示TextField
s的空状态,s1
,s2
,s3
作为复选框的选择状态。这里创建的绑定是((e1&&e2)| |!s1)&&(!s2)&&(!s3)
,也就是说,只要两个文本字段都为空且最后两个复选框未选中,第一个复选框的状态就无关紧要。此外,如果至少选择了一个文本字段和复选框1,则按钮将启用。让e1
和e2
表示文本字段的空状态,并且s1
,s2
,s3
作为复选框的选择状态。这里创建的绑定是((e1&&e2)| |!s1)&&(!s2)&&(!s3)
,也就是说,只要两个文本字段都为空且最后两个复选框未选中,第一个复选框的状态就无关紧要。此外,如果至少选择了一个文本字段和复选框1,则该按钮为enabled.True。使用“Bindings.createBooleanBinding”比使用“or、and、not”链接看起来更可读。我以前没有看到这两种方法之间的区别,但您对这两种方法的解释现在更清楚了。非常感谢,真的。使用“Bindings.createBooleanBinding”比使用“or、and、not”链接看起来更可读。我以前没有看到这两种方法之间的区别,但您对这两种方法的解释现在更清楚了。非常感谢你。