[英]Accessing public methods of a Private Inner Class, from outside the Enclosing class
[英]Calling Enclosing Class methods inside Inner Class
我在内部类中调用封闭类方法,但是出现语法错误。 这是代码:
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);
}
}
}
}
删除方法调用时,错误消失。
此错误的原因是什么,我该如何解决? 问候
您已将方法声明放置在方法声明中。 只需将所有这些声明移到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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.