当光标放在按钮上时,更改按钮的位置

时间:2014-04-11 16:26:15

标签: java swing jbutton actionlistener mouselistener

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

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

} 

2 个答案:

答案 0 :(得分:0)

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

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

} 

答案 1 :(得分:0)

我认为您最好不要在按钮上添加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();
            }
        });
    }
}