【问题标题】:change the position of the button when cursor is placed on it当光标放在按钮上时更改按钮的位置
【发布时间】:2014-05-25 22:07:06
【问题描述】:

所以我要做的是制作按钮,但用户无法点击它,每次用户指向该按钮时,按钮都会改变其位置。 我正在尝试获取光标的位置,然后将其与按钮的位置进行比较,如果它们相等,则按钮将移动到随机位置,但这不起作用... 任何帮助

public void mouseMoved(MouseEvent me)
{
Point p = MouseInfo.getPointerInfo().getLocation();
x=p.x;
y=p.y;
i=b2.getBounds().x;
j=b2.getBounds().y;

Random d=new Random();
a = d.nextInt(200);
b = d.nextInt(200);

if (x==i && y==j){
b2.setLocation(a,b);
}

} 

【问题讨论】:

  • 你能详细说明你想说什么吗?我不明白??对不起,我是 java 新手
  • 对不起,我误读了这个例子。忽略我说的。
  • 如果我是用户,我会选择按钮并按回车键。但是我可能不会成为这样一个愚蠢程序的用户..

标签: java swing jbutton actionlistener mouselistener


【解决方案1】:

b2.getBounds() 包含按钮的 x、y、宽度和高度。在您的代码中,您正在检查鼠标光标何时位于相同的 x y 位置。但是,只有当光标位于按钮的右上角时才会出现这种情况。您真正想要做的是检查光标何时在按钮的范围内。例如:

public void mouseMoved(MouseEvent me)
{
Point p = MouseInfo.getPointerInfo().getLocation();
x=p.x;
y=p.y;
i=b2.getBounds().x;
j=b2.getBounds().y;

Random d=new Random();
a = d.nextInt(200);
b = d.nextInt(200);

boolean withinX = x >= i && x <= i+b2.getBounds().width;
boolean withinY = y >= j && y <= j+b2.getBounds().height;

if (withinX && withinY){
    b2.setLocation(a,b);
}

} 

【讨论】:

    【解决方案2】:

    我认为您最好在按钮上添加MouseListener,然后使用mouseEntered。它将有效地产生与您尝试实现的结果相同的结果

    private Random d = new Random();
    private JButton button = new JButton("Button");
    
    button.addMouseListener(new MouseAdapter(){
        @Override
        public void mouseEntered(MouseEvent e) {
             int a = d.nextInt(200);
             int b = d.nextInt(200);
             button.setLocation(a, b);
        }
    });
    

    对按钮的容器使用MouseMotionListener 的问题是按钮会吞下容器的鼠标事件。所以试图检查鼠标事件的点是否在按钮内是永远不会成功的。

    这是一个测试用例,说明按钮吞噬鼠标事件的含义。你可以看到当鼠标在按钮上以外的任何地方时它会打印出来

    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.util.Random;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class NewSwingTemplate {
    
        private Random d = new Random();
    
        public NewSwingTemplate() {
            JFrame frame = new JFrame();
            JButton button = new JButton("Button");
            MyPanel panel = new MyPanel(button);
            panel.add(button);
            frame.add(panel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(300, 300);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public class MyPanel extends JPanel {
    
            public MyPanel(final JButton button) {
    
                addMouseMotionListener(new MouseMotionAdapter() {
                    public void mouseMoved(MouseEvent e) {
                        if (button.getBounds().contains(e.getPoint())) {
                            System.out.println("contains");
                            int a = d.nextInt(200);
                            int b = d.nextInt(200);
                            button.setLocation(a, b);
                        } else {
                            System.out.println(e.getPoint());
                        }
                    }
                });
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new NewSwingTemplate();
                }
            });
        }
    }
    

    这是一个示例,它可以按照您想要的方式工作。

    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.Random;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class NewSwingTemplate {
        private Random d = new Random();
    
        public NewSwingTemplate() {
            JFrame frame = new JFrame();
            JPanel panel = new JPanel();
            panel.add(createButton());
            frame.add(panel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(300, 300);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        private JButton createButton() {
            final JButton button = new JButton("Button");
            button.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseEntered(MouseEvent e) {
                    int a = d.nextInt(200);
                    int b = d.nextInt(200);
                    button.setLocation(a, b);
                }
            });
            return button;
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new NewSwingTemplate();
                }
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-03
      • 2018-05-15
      • 2017-12-15
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多