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

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条回答 默认 最新

  • _1_1_7_ 2013-08-22 15:07
    关注

    [code="java"]
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Rectangle;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;

    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);

        this.addMouseMotionListener(new MouseMotionListener() {
    
            @Override
            public void mouseMoved(MouseEvent e) {
                Rectangle bound = btn.getBounds();
                int x = e.getX();
                int y = e.getY()-30;
                if (bound.contains(x, y)) {
                    btn.setVisible(true); // 按钮可见
                } else {
                    btn.setVisible(false);
                }
    
            }
    
            @Override
            public void mouseDragged(MouseEvent e) {
    
            }
        });
    
        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);
        btn.setVisible(false);
    }
    
    public static void main(String[] args) {
        new MainFrame();
    }
    

    }[/code]

    同样的效果,不同的实现。
    你那个之所以不行估计是因为button不显示时是不会响应鼠标事件的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗