- 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!");,希望对你有用。
------解决方案--------------------