所以我正在制作国际象棋比赛,但只有骑士。
这是移动骑士的方法
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);
}
}
我的问题是,当我第一次出现新骑士时,我可以移动它。所以我想也许如果我删除动作执行方法中的actionlistener我可以解决这个问题。你怎么看?如果这是一个愚蠢的问题,我很抱歉java。
答案 0 :(得分: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);
我还确保删除动作监听器只是除了一次调用之外,你应该尽量避免编写重复的代码。