在一个窗口上显示一个按钮,一旦鼠标移动到这个按钮上时,按钮就移动到了其他位置的问题
程序代码:
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 name)
{
super(name);
enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
}
protected void processMouseMotionEvent(MouseEvent e)
{
setVisible(false);
friend.setVisible(true);
}
}
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 code here
System.out.println("Starting TestMyButton");
TestMyButton mainFrame = new TestMyButton();
mainFrame.setTitle("TestMyButton!");
mainFrame.setVisible(true);
mainFrame.setSize(500,500);
MyButton btn1= new MyButton("你来抓我啊!");
MyButton btn2= new MyButton("你来抓我啊!");
btn1.setFriend(btn2);
btn2.setFriend(btn1);
mainFrame.add(btn1,"North");
mainFrame.add(btn2,"South");
btn2.setVisible(false);
}
}
运行后鼠标移动到了一个按钮上,但另外那个按钮却没有显示出来,但如果最小化窗口后在打开,一切又正常了。请问各位大虾可知是什么问题?
import java.awt.event.*;
public class MyButton extends Button{
private MyButton friend=null;
public void setFriend(MyButton friend)
{
this.friend = friend;
}
public MyButton(String name)
{
super(name);
enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
}
protected void processMouseMotionEvent(MouseEvent e)
{
setVisible(false);
friend.setVisible(true);
}
}
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 code here
System.out.println("Starting TestMyButton");
TestMyButton mainFrame = new TestMyButton();
mainFrame.setTitle("TestMyButton!");
mainFrame.setVisible(true);
mainFrame.setSize(500,500);
MyButton btn1= new MyButton("你来抓我啊!");
MyButton btn2= new MyButton("你来抓我啊!");
btn1.setFriend(btn2);
btn2.setFriend(btn1);
mainFrame.add(btn1,"North");
mainFrame.add(btn2,"South");
btn2.setVisible(false);
}
}
----------------解决方案--------------------------------------------------------
不好意思,没解决了。不好意思。
----------------解决方案--------------------------------------------------------
把mainFrame.setVisible(true);放到btn2.setVisible(false);前面试试
----------------解决方案--------------------------------------------------------