如何检查 jlabel 数组是否已满?

How to check a jlabel array is full?

如果任何标签已满,这将显示验证弹出窗口。我想更改它,以便它仅在所有 jlabel 都已满时才显示错误消息。

//check to see if car park is full
void checkFull()
{
    for(int i = 0; i < parkingSpace.length; i++)
    {

        if (parkingSpace[i].getIcon() != null)
        {
            JOptionPane.showMessageDialog(null, "Sorry the Car Park is full!");
        }

    }
}

你只需要反转循环的逻辑。如果您找到空的 space,则没有问题 (return)。在循环结束时,如果你还没有 returned,你就没有 spaces left

//check to see if car park is full
void checkFull()
{
    for(int i = 0; i < parkingSpace.length; i++)
    {

        if (parkingSpace[i].getIcon() == null)
        {
          return;
        }

    }
    JOptionPane.showMessageDialog(null, "Sorry the Car Park is full!");

}