为什么我的actionPerformed方法在添加其他代码时停止工作?

时间:2014-10-30 10:02:19

标签: jbutton actionlistener

我正在尝试使用下面的代码为JButton编写actoinPerformed代码。如果我在" count ++"中切断代码,那么一切都可以正常工作。但是,当我添加部件以检查获胜者时,单击按钮时没有任何反应。当我在程序的命令行版本中使用它们时,我正在使用的方法(即placeXToken,placeOToken和checkWinner)都可以正常工作,所以我不认为问题在那里。我认为它可能与尝试在actionPerformed方法中打开JOptionPanes有关,所以(如你所见)我将它们更改为命令行打印,但我仍然遇到同样的问题。当我尝试添加checkWinner部分时,我完全迷失了它为什么停止工作。任何帮助将不胜感激。

@Override
public void actionPerformed(ActionEvent e) {
    String action = e.getActionCommand();
    colInput = Integer.parseInt(action);
    if (count % 2 == 0) {
        instructionslabel.setText("<html>It is " + names[otherIndex] + "'s turn."
                + "<br/>Please select where you would like to place your token.</html");

        placeXToken(colInput);
    }
    else {
        instructionslabel.setText("<html>It is " + names[randIndex] + "'s turn."
                + "<br/>Please select where you would like to place your token.</html");

        placeOToken(colInput);
    }
    count++;

    //Checks to see if there is a winner or a tie each turn
    if (checkWinner() != null) {

        //what to do in case of a tie
        if (checkWinner() == notoken) {
            System.out.println("tie");
            /*int tie = JOptionPane.showConfirmDialog(
                    getParent(), "<html>The game ended in a tie.<br/>Would you like to play again?</html>",
                    "Tie Game",
                    JOptionPane.YES_NO_OPTION);

            if (tie == 0)
                main(null);
            else {
                JOptionPane.showMessageDialog(getParent(),
                        "Thank you for playing.  Goodbye.", "Goodbye.", JOptionPane.PLAIN_MESSAGE);
                close();
            }*/
        }

        //what to do if X wins
        if (checkWinner() == xtoken) {
            System.out.println("xwin");
            /*int xwin = JOptionPane.showConfirmDialog(getParent(),
                    names[randIndex] + " won the game.Would you like to play again?",
                    "X Win",
                    JOptionPane.YES_NO_OPTION);

            if (xwin == 0)
                main(null);
            else {
                JOptionPane.showMessageDialog(getParent(),
                        "Thank you for playing.  Goodbye.", "Goodbye.", JOptionPane.PLAIN_MESSAGE);
                close();
            }*/
        }


        //what to do if O wins
        if (checkWinner()== otoken) {
            System.out.println("owin");
            /*int owin = JOptionPane.showConfirmDialog(getParent(),
                    names[otherIndex] + " won the game.Would you like to play again?",
                    "O Win",
                    JOptionPane.YES_NO_OPTION);

            if (owin == 0)
                main(null);
            else {
                JOptionPane.showMessageDialog(getParent(),
                        "Thank you for playing.  Goodbye.", "Goodbye.", JOptionPane.PLAIN_MESSAGE);
                close();
            }*/         
        }
    }
}

这是checkWiner方法。也许某人可能能够指出我在将其从用于命令行转换为GUI时可能犯的错误。

public static Icon checkWinner() {

    // Checks for horizontal win
    for (int row = 0; row < 7; row = row++) {         
      for (int col = 0; col < 5; col =col++) {
        if ((slotlabels[row][col].getIcon() != notoken)
        && (slotlabels[row][col+1].getIcon() != notoken)
        && (slotlabels[row][col+2].getIcon() != notoken)
        && (slotlabels[row][col+3].getIcon() != notoken)
        && ((slotlabels[row][col].getIcon() == slotlabels[row][col+1].getIcon())
        && (slotlabels[row][col+1].getIcon() == slotlabels[row][col+2].getIcon())
        && (slotlabels[row][col+2].getIcon() == slotlabels[row][col+3].getIcon())))

            return slotlabels[row][col].getIcon();  // returns the token (X or O) of the winner
      }
    }

    // Checks for vertical win
    for (int col = 0; col < 8; col =col++) {      
      for (int row =0; row < 4; row = row++) {
            if((slotlabels[row][col].getIcon() != notoken)
            && (slotlabels[row+1][col].getIcon() != notoken)
            && (slotlabels[row+2][col].getIcon() != notoken)
            && (slotlabels[row+3][col].getIcon() != notoken)
            && ((slotlabels[row][col].getIcon() == slotlabels[row+1][col].getIcon())
        && (slotlabels[row+1][col].getIcon() == slotlabels[row+2][col].getIcon())
        && (slotlabels[row+2][col].getIcon() == slotlabels[row+3][col].getIcon())))

                return slotlabels[row][col].getIcon();  // returns the token (X or O) of the winner 
      }  
    }

    // Checks for diagonal left-top to right-bottom win
    for (int row = 0; row < 4; row = row++) {
      for (int col = 0;col < 5; col = col++) {
            if((slotlabels[row][col].getIcon() != notoken)
            && (slotlabels[row+1][col+1].getIcon() != notoken)
            && (slotlabels[row+2][col+2].getIcon() != notoken)
            && (slotlabels[row+3][col+3].getIcon() != notoken)
            && ((slotlabels[row][col].getIcon() == slotlabels[row+1][col+1].getIcon())
            && (slotlabels[row+1][col+1].getIcon() == slotlabels[row+2][col+2].getIcon())
            && (slotlabels[row+2][col+2].getIcon() == slotlabels[row+3][col+3].getIcon())))

                return slotlabels[row][col].getIcon();  // returns the token (X or O) of the winner
      }  
    }        

    // Checks for diagonal right-top to left-bottom win
    for (int row = 0; row < 4; row = row++) {
      for (int col = 3; col < 8; col = col++) {
            if((slotlabels[row][col].getIcon() != notoken)
            && (slotlabels[row+3][col-4].getIcon() != notoken)
            && (slotlabels[row+6][col-8].getIcon() != notoken)
            && (slotlabels[row+9][col-12].getIcon() != notoken)
            && ((slotlabels[row][col].getIcon() == slotlabels[row+3][col-4].getIcon())
            && (slotlabels[row+3][col-4].getIcon() == slotlabels[row+6][col-8].getIcon())
            && (slotlabels[row+6][col-8].getIcon() == slotlabels[row+9][col-12].getIcon())))

                return slotlabels[row][col].getIcon();  // returns the token (X or O) of the winner
      }  
    }

    // Checks to see if the game resulted in a tie
    boolean tie = true;
    for (int row = 0; row < 7; row = row++) {
        for (int col = 0; col < 8; col = col++) {
            if (slotlabels[row][col].getIcon() == notoken)
                tie = false;
        }
    }
    if (tie == true)
        return notoken;

    // Otherwise, there is no winner and no tie, so do nothing with this.
    return null;
  }

2 个答案:

答案 0 :(得分:0)

在IDE的调试器中启动应用程序,并为行count++设置断点。然后,调试器允许您逐步执行代码,这样您就可以看到执行的每一行,并且还会显示所有可见变量的值。

这样,您应该能够看到代码中断的位置。

如果您没有IDE,那么便宜的解决方法是在任何地方添加System.out.println()一些有用的文字,以便您可以看到输出停止的位置。

最后,您可能希望将代码包装在带有

的大try/catch块中
catch(Exception e) { e.printStackTrace(); }

最后,您可以看到代码可能在控制台上抛出的任何异常。

答案 1 :(得分:0)

我不小心从命令行(我在网格中制作3x4单元格以使其看起来很漂亮)转换到GUI(每个插槽距离前一个只有+1),然后我结束了将for循环中的增量保留为“row = row ++”和“col = col ++”。 Java不喜欢这个。