import java.awt.*;
import java.awt.event.*;
public class MyButton extends Button {
private MyButton friend=null;
public void setFriend(MyButton friend)
{
this.friend=friend;
}
public MyButton(String title){
super(title);
enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
}
protected void processMouseMotionEvent(MouseEvent e){
setVisible(false);
friend.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
public class TestMyButton extends Frame{
public TestMyButton()
{
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
public static void main(String[] args) {
// TODO, add your application code
System.out.println("running TestMyButton!");
TestMyButton mainFrame=new TestMyButton();
MyButton btn1=new MyButton("你来抓我啊1");
MyButton btn2=new MyButton("你来抓我啊2");
btn1.setFriend(btn2);
btn2.setFriend(btn1);
mainFrame.add(btn1,"North");
mainFrame.add(btn2,"South");
mainFrame.setSize(400,400);
mainFrame.setVisible(true);
mainFrame.setTitle("TestMyButton");
btn1.setVisible(false);
}
}
这其中的
private MyButton friend=null;
public void setFriend(MyButton friend)
{
this.friend=friend;
}
与
btn1.setFriend(btn2);
btn2.setFriend(btn1);
的作用不是很理解为什么要这么做有什么作用吗?
如果我理解为这样设置是为了共同调用protected void processMouseMotionEvent(MouseEvent e){
setVisible(false);
friend.setVisible(true);
}
这个方法?这么理解对吗?
望高手指教
[ 本帖最后由 李龙涛 于 2011-07-28 16:01 编辑 ] |
|