当前位置: 代码迷 >> J2SE >> 请问,为什么为死循环
  详细解决方案

请问,为什么为死循环

热度:88   发布时间:2016-04-24 01:01:38.0
请教,为什么为死循环?
Java code
import java.awt.*;import java.awt.event.*;public class MyButton extends Button{    private MyButton MB=null;    public MyButton()    {        enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);    }    public void setfriend(MyButton MB)    {        this.MB=MB;    }    protected void processMouseMotionEvent(MouseEvent e)    {        setVisible(false);        MB.setVisible(true);    }}



Java code
import java.awt.*;import java.awt.event.*;public class TestMyButton extends Frame{    public static void main(String []args)    {        TestMyButton tmb=new TestMyButton();        MyButton mb1=new MyButton();        MyButton mb2=new MyButton();                tmb.add(mb1,"North");        tmb.add(mb2,"South");        tmb.setSize(400,400);        tmb.setVisible(true);        mb1.setfriend(mb2);//谁能给讲解下,此处为什么为死循环;        mb2.setfriend(mb1);//谁能给讲解下,此处为什么为死循环;        mb2.setVisible(false);    }}


------解决方案--------------------
教你个办法,CTRL+C,把java杀掉。因为你没给窗口添加关闭的event监听,所以暂时是关不掉的。想要知道程序是不是执行到最后了,可以在“mb2.setfriend(mb1);”如果加上System.out.println("Mb2 has set a friend named mb1!");,希望对你有用。
------解决方案--------------------
探讨
回答楼上,不是程序停住了,也不是循环语句出现,更不是窗口关闭了,都不是。
你们都没有复制粘贴代码去看。

是当鼠标移到按钮上,面板上两个按钮交替显现,
造成这种现象的语句我已经标明,请问原理是什么。
  相关解决方案