package g;
import java.awt.AWTEvent;
import java.awt.Button;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestFrame extends Frame{
public TestFrame(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}
public static void main(String[] args) {
MyButton b1 = new MyButton("来抓我啊");
MyButton b2 = new MyButton("我在这里");
b1.SetFriend(b2);
b2.SetFriend(b1);
TestFrame tf = new TestFrame();
tf.setTitle("窗口");
tf.setSize(400, 300);
tf.add(b1,"North");
tf.add(b2,"South");
tf.setVisible(true);
b2.setVisible(false);
}
}
class MyButton extends Button{
private MyButton friend;
public void SetFriend(MyButton friend){
this.friend = friend;
}
public MyButton(String label) throws HeadlessException {
super(label);
enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);//注册鼠标事件
}
protected void processMouseMotionEvent(MouseEvent e) {
setVisible(false);
friend.setVisible(true);
}
}