如何在禁用所有按钮后才显示Game Over面板?

时间:2011-05-27 00:30:28

标签: java arrays boolean multidimensional-array

public void actionPerformed(ActionEvent e)
{
    boolean hasProblemsleft = true;
    Object source = e.getSource();
    if(source == quit)
    {
    cards.show(c, "Introduction");
    for(int row = 0; row < 5; row++)
        for(int col = 0; col < 5; col++)
            buttons[row][col].setEnabled(true);
    }
    for(int row = 0; row < 5; row++)
    for(int col = 0; col < 5; col++)
        {
        if(source == buttons[row][col])
        {
            questions.showTimer(row, col);
            buttons[row][col].setEnabled(false); // disables button after user                                                                                               /                                                              //clicks on it
        }
        else if(buttons[row][col].isEnabled())
        {
            hasProblemsleft = false;
            }
        }
        if(hasProblemsleft)
        {
        cards.show(c, "Game Over!");
        }
    }

1 个答案:

答案 0 :(得分:3)

使用一组单独的嵌套for循环(在上面的嵌套循环之后)检查是否每个按钮都被禁用,如果是,则在面板上显示你的游戏。

编辑1:
您可以使用一组嵌套循环在按下按钮后禁用该按钮,并禁用所有按钮的第二组检查。如,

   public void actionPerformed(ActionEvent e) {
      Object source = e.getSource();
      if (source == quit) {
         cards.show(c, "Introduction");
         for (int row = 0; row < 5; row++)
            for (int col = 0; col < 5; col++)
               buttons[row][col].setEnabled(true);
      } else { // don't forget this important else!
         for (int row = 0; row < 5; row++) { // use curly braces for *ALL*
                                             // loops/blocks
            for (int col = 0; col < 5; col++) {
               if (source == buttons[row][col]) {
                  questions.showTimer(row, col);
                  buttons[row][col].setEnabled(false);
               }
            }
         }

         boolean done = true;
         for (int row = 0; row < 5; row++) {
            for (int col = 0; col < 5; col++) {
               if (buttons[row][col].isEnabled()) {
                  done = false;
                  break;
               }
            }
         }
         if (done) {
            cards.show(c, "Game Over!");
         }
      }
   }