简体   繁体   中英

Calling Enclosing Class methods inside Inner Class

I am calling the enclosing class method inside inner class, but I get syntax error. Here is the code:

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);
      }

    }
  }

}

The error goes away when remove the method call.

What is the reason for this error and how can I solve it? Regards

You have placed method declarations inside a method declaration. Just move all those declaration outside 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
  }

By the way, I hope you are using an IDE to develop Java code. If so, find out how to automatically reformat the code and use this feature regurarly. In Eclipse it ist Ctrl-Shift-F.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM