import java.awt.*;
import java.awt.event.*;
class TestButton  extends Button
{

private Button btnFriend;
static Frame f;
public TestButton(String s)
{
super(s);
enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
}

public void setFriend(Button btn)
{
btnFriend=btn;
}

protected void processMouseMotionEvent(MouseEvent e)
{
if(this.isVisible()){
this.setVisible(false); f.show();

}
}


public static void main(String args[])
{
TestButton btn1=new TestButton("你来");
TestButton btn2=new TestButton("你来");

btn1.setFriend(btn2);
btn2.setFriend(btn1);
btn1.setVisible(false);


f=new Frame("IT");
f.add(btn1,BorderLayout.NORTH );
f.add(btn2,BorderLayout.SOUTH);
f.setSize(300,400);
f.show();


}

}

解决方案 »

  1.   

    前面贴错了,应该是这个import java.awt.*;
    import java.awt.event.*;
    class TestButton  extends Button
    {

    private Button btnFriend;
    static Frame f;
    public TestButton(String s)
    {
    super(s);
    enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
    }

    public void setFriend(Button btn)
    {
    btnFriend=btn;
    }

    protected void processMouseMotionEvent(MouseEvent e)
    {
    if(this.isVisible()){
    this.setVisible(false);
    btnFriend.setVisible(true);
    f.show();

    }
    }


    public static void main(String args[])
    {
    TestButton btn1=new TestButton("你来");
    TestButton btn2=new TestButton("你来");

    btn1.setFriend(btn2);
    btn2.setFriend(btn1);
    btn1.setVisible(false);


    f=new Frame("IT");
    f.add(btn1,BorderLayout.NORTH );
    f.add(btn2,BorderLayout.SOUTH);
    f.setSize(300,400);
    f.show();


    }

    }
      

  2.   

    为什么要加 f.show(); 当前窗口不是一直显示的吗?
      

  3.   

    to galois_godel() :
    为什么要加 f.show(); 当前窗口不是一直显示的吗?
      

  4.   

    窗口不显式调用show()或setVisible(ture)是不会显示的!
      

  5.   

    这样就OK了
    import java.awt.*;
    import java.awt.event.*;class TestButton extends Button
    {

    private Button btnFriend;

    public TestButton(String s)
    {
    super(s);
    enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
    }

    public void setFriend(Button btn)
    {
    btnFriend=btn;
    }

    protected void processMouseMotionEvent(MouseEvent e)
    {

    this.setVisible(false);
    btnFriend.setVisible(true);
    }

    public static void main(String args[])
    {
    TestButton btn1=new TestButton("你来");
    TestButton btn2=new TestButton("你来");

    btn1.setFriend(btn2);
    btn2.setFriend(btn1);
    //btn1.setVisible(false);


    Frame f=new Frame("IT");
    f.add(btn1,BorderLayout.NORTH );
    f.add(btn2,BorderLayout.SOUTH);
    f.setSize(300,400);
    f.show();


    }

    }
      

  6.   

    import java.lang.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;class MyButton extends Button
    { public MyButton friend;
    public void setFriend(MyButton friend)
    {
    this.friend = friend;
    }

    public MyButton(String name)
    {
    super(name);
    enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);

    }
    public void processMouseMotionEvent(MouseEvent e)
    {
    this.setVisible(false);
    friend.setVisible(true);

    }

    }
    public class TextButton extends Frame 
    {
    public TextButton()
    {
    MyButton btn1=new MyButton("来抓我啊!");
    MyButton btn2=new MyButton("来抓我啊!");
    btn1.setFriend(btn2);
    btn2.setFriend(btn1);
      this.add(btn1,"South");
      this.add(btn2,"North");
    this.setSize(300,300);
    this.setBackground(Color.orange);
    this.setVisible(true);
    this.addWindowListener(new closeframe());
    btn1.setVisible(false);
    }

    public static void main(String [] args)
    {
    new TextButton();
    }


    class closeframe extends WindowAdapter
    {
    public void windowClosing(WindowEvent e)
    {
    JOptionPane.showMessageDialog(null,"确定要退出吗?");
    System.exit(0);
    }

    }

     


      

  7.   

    TO:TO:TO:TO:TO:TO:   楼主!!!!!!!!!!!
    你只要把你的main()函数中的btn1.setVisible(false);该成btn.setVisible(false);
    然后将它放到f.show();的下一行就完全ok了!