我有 5 个复选框,分别是星期一、星期二、星期三、星期四、星期五。当任何一个被选中时,我试图从这些复选框中获取值。但我意识到,当所有这些都没有被选中时,当我打印它时,那些没有被选中的有空值。我怎样才能只打印选择的值,而两者之间没有空值。那就是当我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 = "";
}
}