Mouseexited java,使用GUI多次后,在java.awt.AWTEventMulticaster.mouseExited(未知源)发生...

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

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

解决方案

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 !!

}

}

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.

Solution:

Don't do this. Don't add the same listener to a component inside of its listener code.

Again, don't use MouseListeners at all for JButtons. Use ActionListeners.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值