how to remove an action listener?

二次信任 提交于 2019-12-02 08:20:37

问题


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


回答1:


Like nachokk said, you should use: component.removeActionListener(theActionListenerYouWantToRemove)

Here is how you can use it in your e method:

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



来源:https://stackoverflow.com/questions/17225628/how-to-remove-an-action-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!