2013-06-20 98 views
1

所以即时制作国际象棋游戏,但只与骑士。如何删除动作监听器?

这是移动骑士

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对不起,如果这是一个愚蠢的问题

+6

你删除actionListener像这样.. component.removeActionListener(theActionListenerYouWantToRemove) – nachokk

+0

为了更好的帮助,请发布[SSCCE](http://sscce.org/)。 –

回答

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);我还确保remove action动作监听器只被调用一次,你应该避免重复编写代码尽可能。