使用GUI多次后,在java.awt.AWTEventMulticaster.mouseExited(未知源)发生 [英] at java.awt.AWTEventMulticaster.mouseExited(Unknown Source) occur after using the GUI for several times

查看:411
本文介绍了使用GUI多次后,在java.awt.AWTEventMulticaster.mouseExited(未知源)发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,我正在为游戏实现GUI,当我玩游戏一段时间后,我得到了无数的这种异常,然后游戏死机了,任何有关问题或解决方法的帮助都很大感激

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

下面是代码:

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);
    }




}

唯一的例外按摩我只能得到无穷无尽的次数 在java.awt.AWTEventMulticaster.mouseExited(未知来源)

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

我非常确定董事会课程是100%的,因为它是由我们大学的助教制作的,并且通过了所有测试

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

预先感谢

推荐答案

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

I see a potential source of problems:

     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两次,并添加两个 MouseListener,然后依次是 4 8 16 ,...这将导致每次调用时添加的MouseListener数量增加 几何形状 ,并且很快会使您的系统不堪重负.

I was keyed in that your error involved Swing mouse handling. You appear to be adding a MouseListener multiple times to your components. So picture this, when the MouseListener is called, it adds another MouseListener to the same component. With the next mousepress, the MouseListener will be called twice and two MouseListeners will be added, then 4, then 8, then 16, ... This will lead to a geometric increase in the number of MouseListeners added whenever one is called, and will soon overwhelm your system.

解决方案:

  1. 不要这样做.请勿在侦听器代码内的组件中添加相同的侦听器.
  2. 同样,不要对JButton使用MouseListeners.使用ActionListeners.

这篇关于使用GUI多次后,在java.awt.AWTEventMulticaster.mouseExited(未知源)发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆