java 如何删除动作侦听器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17225628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to remove an action listener?
提问by user2488345
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
我的问题是,当我第一次移动这件作品时,新骑士出现时我以前可以移动它。所以我在想,也许如果我删除 action perform 方法中的 actionlistener,我可以解决这个问题。你怎么认为?如果这是一个愚蠢的问题,我是 Java 新手抱歉
回答by John McDonnell
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);
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.
我看到您是 Java 新手,您会注意到我稍微修改了您的 e 方法以仅调用,current.setIcon(null);
并且((AbstractButton) e.getSource()).setEnabled(false);
我还确保删除操作侦听器也仅被调用一次,您应该避免编写重复的代码尽可能。