问题描述
BooleanBinding bb = new BooleanBinding() {
{
super.bind(addnum.textProperty(),addt1.textProperty(),addt2.textProperty(),
addt3.textProperty(),addasg.textProperty(),addatt.textProperty());
}
@Override
protected boolean computeValue() {
return (addnum.getText().isEmpty() && addt1.getText().isEmpty()
&& addt2.getText().isEmpty() && addt3.getText().isEmpty()
&& addasg.getText().isEmpty() && addatt.getText().isEmpty());
}
};
final Button b2 = new Button("Add");
b2.disableProperty().bind(bb);
这是我的代码,当TextFields为空时禁用Button,即Button变为全部激活时TextField已填充。但是这段代码不起作用。填充一个TextField时,Button变为活动状态。我在我的项目的其他部分使用此代码为此目的添加它在那里工作正常。为什么不在这里工作?
This is my code to disable Button when the TextFields are empty, that is the Button becomes active when all the TextField are filled. But this code is not working. When one TextField is filled the Button becomes active. I had used this code at other parts of my project for this same purpose add it is working fine there. Why is it not working here ?
推荐答案
如果你想要按钮
当且仅当所有字段都被填充(即非空)时,才能激活,那么您使用的是错误的运算符。使用 ||
代替&&
使其正常工作。
If you want the Button
to be active if and only if all fields are filled (i.e. not empty), then you are using the wrong operator. Use ||
instead of &&
to make it work.
如果你使用DeMorgan的法律从 computeValue
重新制定公式,你可以很容易地看出什么是错的;我写了
You can easyly see what's wrong, if you reformulate the formula from computeValue
using DeMorgan's laws; I write
a1, a2, ..., a6
而不是
`addnum.getText().isEmpty()`, `addt1.getText().isEmpty()`, ..., `addatt.getText().isEmpty()`:
以下陈述是等价的:
- 按钮处于活动状态
-
!(a1&& a2&& ...&& a6)
-
(!a1 ||!a2 || ... ||!a6)
- 至少有一个字段是已填充
- the button is active
!(a1 && a2 && ... && a6)
(!a1 || !a2 ||...|| !a6)
- at least one field is filled
与 ||
而不是&&
:
In contrast to that with ||
instead of &&
:
以下陈述是等效的:
- 按钮处于活动状态
-
!(a1 || a2 || ... || a6)
-
(!a1&&!a2&& ...&& ;!a6)
- 所有字段都已填写
- the button is active
!(a1 || a2 || ... || a6)
(!a1 && !a2 &&...&& !a6)
- all fields are filled
这篇关于在TextFields为空时不起作用时禁用Button的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!