0

我在内部类中调用封闭类方法,但出现语法错误。这是代码:

public class Horizon
{
  ....
  public void checkWinner()
  {
    if(x[1]==1 && x[2]==1 && x[3]==1 || x[4]==1 && x[5]==1 && x[6]==1 || x[7]==1 && x[8]==1 && x[9]==1)
    {
      JOptionPane.showMessageDialog(null, "Congratulations!\n      Player 1 won!", "Congratulations",JOptionPane.OK_OPTION);
    }
  }
  private class HandlerClass implements MouseListener
  {
    public void mouseClicked(MouseEvent event)
    {

      if(event.getSource()==lb1)
      {
        if(j==1)
          x[1]=1;
        else
          x[1]=2;
        lb1.setIcon(icon);
      }
      public void mousePressed(MouseEvent event){}
      public void mouseEntered(MouseEvent event){}
      public void mouseReleased(MouseEvent event){}
      public void mouseExited(MouseEvent event){} // error here

      Horizon.this.checkWinner(); 

      if(i==9)
      {
        JOptionPane.showMessageDialog(null, "The game is a draw", "Draw", JOptionPane.OK_OPTION);
      }

    }
  }

}

删除方法调用后错误消失。

这个错误的原因是什么,我该如何解决?问候

4

1 回答 1

0

您已将方法声明放在方法声明中。只需将所有这些声明移到外面mouseClicked

  private class HandlerClass implements MouseListener
  {
    public void mouseClicked(MouseEvent event)
    {
      if(event.getSource()==lb1)
      {
        if(j==1)
          x[1]=1;
        else
          x[1]=2;
        lb1.setIcon(icon);
      }

      Horizon.this.checkWinner(); 

      if(i==9)
      {
        JOptionPane.showMessageDialog(null, "The game is a draw", "Draw", JOptionPane.OK_OPTION);
      }
    }

    public void mousePressed(MouseEvent event){}
    public void mouseEntered(MouseEvent event){}
    public void mouseReleased(MouseEvent event){}
    public void mouseExited(MouseEvent event){} // error here
  }

顺便说一句,我希望您使用 IDE 来开发 Java 代码。如果是这样,请了解如何自动重新格式化代码并定期使用此功能。在 Eclipse 中,它是 Ctrl-Shift-F。

于 2012-11-13T15:15:52.267 回答