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

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

相关推荐 更多相似问题