此代码获取选中了多少个复选框(nchecked)。当选择超过 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()));
提前致谢!!
最佳答案
也许当你调用这条线路时
if (checkBoxes.get(index).isSelected())
如果索引处的框被选中,则仅执行以下 if/else 语句。如果没有检查,您将不会执行这些语句,因此不会打印警告
关于java - 未选中任何复选框时不会输出警告。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44575337/