问题
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