这是代码:
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(未知来源)
我非常确定董事会班级是100%,因为它是由我们大学的老师助理制作的,并通过了所有测试
提前致谢
答案 0 :(得分: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添加到同一个组件中。使用下一个mousepress,MouseListener将被调用两次,两个 MouseListeners将被添加,然后 4 ,然后 8 ,然后 16 ,...每当调用一个MouseListener时,这将导致 几何增加 ,并且很快会使您的系统崩溃。
解决方案: