简体   繁体   English

如何删除动作监听器?

[英]how to remove an action listener?

so im making a chess game but only with the knight. 因此,我只与骑士进行棋牌游戏。

this is the method for moving the knight 这是移动骑士的方法

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

    }
}

my problem is that when i move the piece the first time new knights appear were I could have move it before. 我的问题是,当我第一次搬动这块棋子时,如果我本来可以把它移开的话。 So i was thinking maybe if i remove the actionlistener inside the action perform method i could fix this. 所以我在想,如果我删除动作执行方法中的动作侦听器,则可以解决此问题。 What do you think? 你怎么看? Im new to java sorry if this is a stupid question 我是java的新手,如果这是一个愚蠢的问题,对不起

Like nachokk said, you should use: component.removeActionListener(theActionListenerYouWantToRemove) 就像nachokk所说的,您应该使用: component.removeActionListener(theActionListenerYouWantToRemove)

Here is how you can use it in your e method: 这是您可以在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);
        }
    };
}

I see that you are new to Java, you'll notice that I have slightly modified your e method to only call current.setIcon(null); 我看到您是Java的新手,您会注意到我对e方法进行了一些修改,使其仅调用current.setIcon(null); and ((AbstractButton) e.getSource()).setEnabled(false); ((AbstractButton) e.getSource()).setEnabled(false); I have also made sure that the remove action listener is only called once aswell, you should look to avoid writing duplicate code as much as possible. 我还确保了移除动作监听器也仅被调用过一次,您应该避免编写尽可能多的重复代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM