-3

所以我正在编写一个相当低级别的Java编程程序。这就是我遇到的问题:

//The String fillText is given a value earlier in the program
if ("".equals(txa1.getText()))
{
    txa1.setText(fillText);
    txa1.setVisible(true);
}
else if ("".equals(txa2.getText()))
{
    txa2.setText(fillText);
    txa2.setVisible(true);
}
else if ("".equals(txa3.getText()))
{
    txa3.setText(fillText);
    txa3.setVisible(true);
}
else if ("".equals(txa4.getText()))
{
    txa4.setText(fillText);
    txa4.setVisible(true);
}
else if ("".equals(txa5.getText()))
{
    txa5.setText(fillText);
    txa5.setVisible(true);
}
...

此代码似乎总是用 fillText 填充所有文本区域(txaX)。我希望它只执行第一个返回 true 的语句,然后跳出 if-else-statement。

我尝试使用 switch-case 来完成它,但最终失败了,因为在程序运行期间更改了字符串。

怎么了?

提前致谢!

4

3 回答 3

0
"".equals(txa1.getText())

我认为每个条件的上述条件都返回真。

getText()方法总是返回空字符串,即“”;

于 2013-03-20T16:59:02.673 回答
0

它在循环中。肯定是导致问题的原因。没有循环,它只会进入一个块。没有循环也不可能执行。当我们使用 if else 时,只会执行一个块。

于 2013-03-20T17:00:25.853 回答
0

你必须仔细检查你的条件,如果条件为假,它基本上会执行前辈。我建议您多考虑一下您要达到的目标的逻辑..

于 2013-03-20T17:37:41.390 回答