该代码获取选中多少个复选框(未选中)。它在超过2个框被选中时工作,但当没有选中复选框时不会输出警告。 应该有什么不同?没有选择复选框时没有输出警告。
下面是获取复选框
private int getCheckBoxes(){
int count = 0;
for (int i = 0; i < checkBoxes.size(); i++){
if (checkBoxes.get(i).isSelected()) {
count++;
}
}
return count;
}
private int getIndex(String cityName) {
return cities.indexOf(cityName);
}
//Here is the code that I'm putting out my warning
private void handleCitySelection (int index) {
if (checkBoxes.get(index).isSelected())
{
int nchecked = getCheckBoxes();
if (nchecked <= 0)
{
outputDistance.append("Please Select 2 Cities");
}
if(nchecked >= 3)
{
checkBoxes.get(index).setSelected(false);
}
else
if (nchecked == 1) {
city1 = index;
}
else
{
city2 = index;
}
}
}
还落后每个复选框是代码:
handleCitySelection(getIndex(evt.getActionCommand()));
在此先感谢!
那么,因为您的警告只显示在选中复选框时,您需要某种方式将您的警告代码绑定到原来的状态当你想检查没有选择框时发生。试试[mcve] –