一个代码显示效果不是所想要的,求助
图形界面上有一个按钮,当鼠标移到按钮上时按钮掩藏,当鼠标离开按钮时按钮显示。下面是我的代码,不过不是我想要的效果,大侠们帮忙改正一下,最好是先说下思路,说一下我的思路为什么错了。程序代码:
import java.awt.*; import java.awt.event.*; public class shu145_8 extends Frame implements WindowListener{ Button b; Panel p; public shu145_8(String title){ super(title); b=new Button("按钮"); p=new Panel(); p.add(b,"Center"); add(p); addWindowListener(this); b.addMouseListener(new MouseHandler()); } public static void main(String args[]){ shu145_8 me=new shu145_8("鼠标"); me.setSize(200,200); me.setVisible(true); } public void windowClosing(WindowEvent e){ System.exit(0); } public void windowClosed(WindowEvent e){} public void windowDeactivated(WindowEvent e){} public void windowActivated(WindowEvent e){} public void windowIconified(WindowEvent e){} public void windowDeiconified(WindowEvent e){} public void windowOpened(WindowEvent e){} class MouseHandler implements MouseListener{ public void mouseClicked(MouseEvent e){} public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void mouseExited(MouseEvent e){ b.setVisible(true); } public void mouseEntered(MouseEvent e){ b.setVisible(false); } } }如果我在开始就按钮掩藏
b.setVisible(false);
然后再运行,效果是鼠标到按钮上就消失了,不过鼠标离开按钮时,按钮也不显示出来了,