我在内部类中调用封闭类方法,但是我得到语法错误。这是代码:
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);
}
}
}
}
删除方法调用时,错误消失。
出现此错误的原因是什么?如何解决? 此致
答案 0 :(得分: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。