所以我正在編寫一個相當低水平的Java編程的程序。 這是我有什麼用的問題:Java如果-else語句沒有按預期工作
//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);
}
...
此代碼似乎總是填滿所有的文字區域(txaX)用的fillText方法。 我期待它只執行第一個返回true的語句,然後突破if-else語句。
我試圖用switch-case做,但最終失敗了,因爲在程序運行期間字符串被改變了。
出了什麼問題?
在此先感謝!
它是在這些文本字段的循環或監聽器? – 2013-03-20 16:57:07
看起來問題在於代碼中的其他地方,而不是在這裏。檢查此代碼是否在程序中多次執行(例如在循環中或在某個地方的不同調用中)。 – 2013-03-20 16:57:13
你可以解釋清楚pls ..... – PSR 2013-03-20 16:58:54