2

Good Morning, i'm implementing the GUI for a game and when i play the game for sometime i get an endless number of this exception then the game freezes, any help on what is the problem or how to fix it is much appreciated

here is the code:

public class BoardFrame extends JFrame implements MouseListener {


    private void boardWithoutCheckers() {


        for(int i=0; i<8; i++) {
            for(int j=0; j< 8; j++) {
                if(((i + j) % 2) == 0){
                    boardFrame[i][j] = new LightGrayButton();

                    }
                    else {
                        boardFrame[i][j] = new DarkGrayButton();
                    }
                boardFrame[i][j].addMouseListener(this);

                this.getContentPane().add(boardFrame[i][j]);
            }

        }
        this.setVisible(true);
    }



@Override
public void mouseClicked(MouseEvent e) {



    count++;
    if(count == 1){
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            if(e.getSource().equals(boardFrame[i][j])){
                possibleMoves = board.getPossibleMoves(new Point(j,i));
            for (int k = 0; k < possibleMoves.size(); k++) {

                Point temp = new Point(possibleMoves.get(k).getX(),possibleMoves.get(k).getY());
                boardFrame[temp.getY()][temp.getX()].setBackground(new Color(99,204,94,50));
            }
            firstClick = new Point(j, i);
            break;
            }
            }
        }

    }
    if(count == 2){

        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if(e.getSource().equals(boardFrame[i][j])){


                for (int k = 0; k < possibleMoves.size(); k++) {
                    if(possibleMoves.get(k).getX() == j && possibleMoves.get(k).getY() == i){
                        if(board.getTurn() == 1){
                        boardFrame[i][j].setIcon(null);
                        boardFrame[i][j].setIcon(new ImageIcon(Earth));
                        boardFrame[firstClick.getY()][firstClick.getX()].setIcon(null);
                        board.move(firstClick, new Point(j,i));



                        }
                        else if(board.getTurn() == 2){
                            boardFrame[i][j].setIcon(null);
                            boardFrame[i][j].setIcon(new ImageIcon(Mars));
                            boardFrame[firstClick.getY()][firstClick.getX()].setIcon(null);
                            board.move(firstClick, new Point(j,i));

                            break;
                        }


                }

                }
                }
                }

    }

        count=0;
        possibleMoves = new ArrayList<Point>();
        for(int i=0; i<8; i++) {
            for(int j=0; j< 8; j++) {
                if(((i + j) % 2) == 0){
                    boardFrame[i][j].setBackground(new Color(15, 81, 162));

                    }
                    else {
                        boardFrame[i][j].setBackground(new Color(77, 77, 77));
                    }
                boardFrame[i][j].addMouseListener(this);
            }

    }
    }

    if(board.isGameOver()){
        JLabel winner = new JLabel("we have a winner");
        this.getContentPane().add(winner);
    }




}

The only exception massage i get only an endless number of it at java.awt.AWTEventMulticaster.mouseExited(Unknown Source)

im pretty sure that the board class is 100% as it is made by the teacher assistants in our university and it passed all the test

Thanks in advance

4

1 回答 1

4

我看到了潜在的问题根源:

     for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
           if (((i + j) % 2) == 0) {
              boardFrame[i][j].setBackground(new Color(15, 81, 162));

           } else {
              boardFrame[i][j].setBackground(new Color(77, 77, 77));
           }
           boardFrame[i][j].addMouseListener(this); // !! here !!
        }

     }

我被告知您的错误涉及 Swing 鼠标处理。您似乎多次向组件添加 MouseListener。所以想象一下,当调用 MouseListener 时,它会向同一个组件添加另一个 MouseListener。随着下一次鼠标按下,MouseListener 将被调用两次并添加两个MouseListeners,然后是4,然后是8,然后是16,......这将导致每次调用时添加的 MouseListeners 数量呈几何级数增加,并且将很快就会使您的系统不堪重负。

解决方案:

  1. 不要这样做。不要将相同的侦听器添加到其侦听器代码中的组件。
  2. 同样,不要将 MouseListeners 用于 JButtons。使用动作监听器。
于 2013-05-09T16:48:38.567 回答