1

所以我在做一个国际象棋游戏,但只和骑士一起玩。

这是移动骑士的方法

public void caballo(final int row, final int column) {

        final JButton current = mesa[row][column];

        current.setIcon(image);
        panel.repaint();

        acciones(row, column, current);
    }

    public void acciones(final int row, final int column, final JButton current) {

        for (int i = 0; i < HEIGHT; i++) {
            for (int j = 0; j < WIDTH; j++) {
                mesa[i][j].addActionListener(e(row, column, current));



            }
        }
    }

    public ActionListener e(final int row, final int column,
            final JButton current) {
        return new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if (tienebotton(row + 2, column + 1)) {
                    if (e.getSource() == mesa[row + 2][column + 1]) {

                        current.setIcon(null);
                        caballo(row + 2, column + 1);
                        ((AbstractButton) e.getSource()).setEnabled(false);

                    }
                }
                if (tienebotton(row + 2, column - 1)) {
                    if (e.getSource() == mesa[row + 2][column - 1]) {

                        current.setIcon(null);
                        caballo(row + 2, column - 1);

                        ((AbstractButton) e.getSource()).setEnabled(false);

                    }
                }
                if (tienebotton(row - 2, column - 1)) {
                    if (e.getSource() == mesa[row - 2][column - 1]) {

                        current.setIcon(null);
                        caballo(row - 2, column - 1);

                        ((AbstractButton) e.getSource()).setEnabled(false);

                    }
                }
                if (tienebotton(row - 2, column + 1)) {
                    if (e.getSource() == mesa[row - 2][column + 1]) {

                        current.setIcon(null);
                        caballo(row - 2, column + 1);

                        ((AbstractButton) e.getSource()).setEnabled(false);

                    }
                }

                if (tienebotton(row + 1, column + 2)) {
                    if (e.getSource() == mesa[row + 1][column + 2]) {

                        current.setIcon(null);
                        caballo(row + 1, column + 2);

                        ((AbstractButton) e.getSource()).setEnabled(false);

                    }
                }
                if (tienebotton(row - 1, column + 2)) {
                    if (e.getSource() == mesa[row - 1][column + 2]) {

                        current.setIcon(null);
                        caballo(row - 1, column + 2);

                        ((AbstractButton) e.getSource()).setEnabled(false);

                    }
                }
                if (tienebotton(row + 1, column - 2)) {
                    if (e.getSource() == mesa[row + 1][column - 2]) {

                        current.setIcon(null);
                        caballo(row + 1, column - 2);

                        ((AbstractButton) e.getSource()).setEnabled(false);

                    }
                }
                if (tienebotton(row - 1, column - 2)) {
                    if (e.getSource() == mesa[row - 1][column - 2]) {

                        current.setIcon(null);
                        caballo(row - 1, column - 2);

                        ((AbstractButton) e.getSource()).setEnabled(false);

                    }
                }
            }
        };
    }

    public boolean tienebotton(int row, int column) {
        return (row >= 0 && row < HEIGHT && column >= 0 && column < WIDTH);

    }
}

我的问题是,当我第一次移动新骑士出现时,我之前可以移动它。所以我在想,如果我删除动作执行方法中的动作监听器,我可以解决这个问题。你怎么看?如果这是一个愚蠢的问题,我是 Java 新手,抱歉

4

1 回答 1

2

就像 nachokk 说的,你应该使用:component.removeActionListener(theActionListenerYouWantToRemove)

以下是如何在您的e方法中使用它:

public ActionListener e(final int row, final int column,
        final JButton current) {
    return new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            current.setIcon(null);
            if (tienebotton(row + 2, column + 1)) {
                if (e.getSource() == mesa[row + 2][column + 1]) {
                    caballo(row + 2, column + 1);
                }
            }
            if (tienebotton(row + 2, column - 1)) {
                if (e.getSource() == mesa[row + 2][column - 1]) {
                    caballo(row + 2, column - 1);
                }
            }
            if (tienebotton(row - 2, column - 1)) {
                if (e.getSource() == mesa[row - 2][column - 1]) {
                    caballo(row - 2, column - 1);
                }
            }
            if (tienebotton(row - 2, column + 1)) {
                if (e.getSource() == mesa[row - 2][column + 1]) {
                    caballo(row - 2, column + 1);
                }
            }

            if (tienebotton(row + 1, column + 2)) {
                if (e.getSource() == mesa[row + 1][column + 2]) {
                    caballo(row + 1, column + 2);
                }
            }
            if (tienebotton(row - 1, column + 2)) {
                if (e.getSource() == mesa[row - 1][column + 2]) {
                    caballo(row - 1, column + 2);
                }
            }
            if (tienebotton(row + 1, column - 2)) {
                if (e.getSource() == mesa[row + 1][column - 2]) {
                    caballo(row + 1, column - 2);
                }
            }
            if (tienebotton(row - 1, column - 2)) {
                if (e.getSource() == mesa[row - 1][column - 2]) {
                    caballo(row - 1, column - 2);
                }
            }
            ((AbstractButton) e.getSource()).setEnabled(false);
            ((AbstractButton) e.getSource()).removeActionListener(this);
        }
    };
}

我看到您是 Java 新手,您会注意到我稍微修改了您的 e 方法以仅调用current.setIcon(null);,并且((AbstractButton) e.getSource()).setEnabled(false);我还确保删除操作侦听器也只调用一次,您应该避免编写重复的代码尽可能。

于 2013-06-21T21:14:28.560 回答