一于
2013-08-22 14:07
浏览 250
已采纳

Java MouseListener

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;


public class MainFrame extends JFrame {
    public MainFrame(){
        final JButton btn = new JButton("把鼠标放上去出现。。");
        btn.setPreferredSize(new Dimension(200,20));
        btn.setLocation(100, 100);
        btn.setVisible(false);
        btn.addMouseListener(new MouseListener(){

            @Override
            public void mouseClicked(MouseEvent arg0) {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void mouseEntered(MouseEvent arg0) {
                // TODO Auto-generated method stub
                btn.setVisible(true); //按钮可见
            }

            @Override
            public void mouseExited(MouseEvent arg0) {
                // TODO Auto-generated method stub
                btn.setVisible(false);//按钮不可见
            }

            @Override
            public void mousePressed(MouseEvent arg0) {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void mouseReleased(MouseEvent arg0) {
                // TODO Auto-generated method stub
                
            }
            
            
            
        });
        this.setLayout(new FlowLayout(FlowLayout.CENTER));
        this.add(btn);
        this.setBounds(300, 300, 300, 300);
        this.setVisible(true);
        this.validate();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

这类实现的是当鼠标放在按钮上时,按钮出现,移走是隐藏,可是运行起来不是这样的??鼠标移上去没有反应??
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

相关推荐 更多相似问题