我有5個複選框,分別是星期一,星期二,星期四,星期四,星期五。 我試圖從這些複選框中選擇任何一個時獲取Value。 但是我意識到,當他們都沒有被選中時,沒有被選中的那些打印時就會有空值。我怎麼才能打印只有選中一個沒有null值之間的值。這是當我做System.out.print();
假設我選擇星期二和星期四獨自我得到的響應像null tuesday null thursday null
從複選框中獲取值
以下是我所做的。
這是代碼
private void jButton2MouseClicked(java.awt.event.MouseEvent evt)
{
System.out.print(mon+ " " +tue+" "+wed +" "+thus+" " +fri);
JOptionPane.showMessageDialog(null, mon+ " " +tue+" "+wed +" "+thus+" " +fri);
}
private void mondayBoxItemStateChanged(java.awt.event.ItemEvent evt) {
if(mondayBox.isSelected())
{
mon = mondayBox.getText();
}
else
{
mon = " ";
}
}
private void tuesdayBoxItemStateChanged(java.awt.event.ItemEvent evt)
{
if(tuesdayBox.isSelected())
{
tue = tuesdayBox.getText();
}
else
{
tue = "";
}// TODO add your handling code here:
}
private void wedBoxItemStateChanged(java.awt.event.ItemEvent evt) {
if(wedBox.isSelected())
{
wed = wedBox.getText();
}else
{
wed = "";
} // TODO add your handling code here:
}
private void thurBoxItemStateChanged(java.awt.event.ItemEvent evt) {
if(thurBox.isSelected())
{
thus = thurBox.getText();
}else
{
thus = "";
} // TODO add your handling code here:
}
private void friBoxItemStateChanged(java.awt.event.ItemEvent evt) {
if(friBox.isSelected())
{
fri = friBox.getText();
}else
{
fri = "";
}
}
爲什麼不將所有ItemListeners組合到一個ItemListener? – Vallentin
檢查值是否爲空然後打印它們 –