简体   繁体   English

如何获取鼠标单击相对于摆动窗口的位置

[英]How to get location of a mouse click relative to a swing window

Say I'm in a Java Swing JFrame.假设我在 Java Swing JFrame 中。 I click my mouse.我点击我的鼠标。 I want to get the location of the mouse click within the GUI .我想在 GUI 中获取鼠标单击的位置。 In java, the line在java中,该行

int mouseX = MouseInfo.getPointerInfo().getLocation.x;

Seems to give the location of the mouse on the entire screen.似乎给出了鼠标在整个屏幕上的位置。 How would I get it's location relative to the GUI?我如何获得它相对于 GUI 的位置?

From MouseListener methods you can do:MouseListener方法中,您可以执行以下操作:

@Override
public void mouseClicked(MouseEvent e) {
    int x=e.getX();
    int y=e.getY();
    System.out.println(x+","+y);//these co-ords are relative to the component
}

Simply add this to your Component by:只需通过以下方式将其添加到您的Component

component.addMouseListener(new MouseListener() {
    @Override
    public void mouseClicked(MouseEvent e) {
    }
});

Reference:参考:

You can add MouseListener to GUI component whose top left pixel should be threated as [0,0] point, and get x and y from MouseEvent您可以将MouseListener添加到 GUI 组件,其左上角像素应作为 [0,0] 点受到威胁,并从 MouseEvent 中获取 x 和 y

JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.add(panel);
panel.addMouseListener(new MouseAdapter() {// provides empty implementation of all
                                           // MouseListener`s methods, allowing us to
                                           // override only those which interests us
    @Override //I override only one method for presentation
    public void mousePressed(MouseEvent e) {
        System.out.println(e.getX() + "," + e.getY());
    }
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);

Take a look at Component.getMousePosition .看看Component.getMousePosition

Returns the position of the mouse pointer in this Component 's coordinate space if the Component is directly under the mouse pointer, otherwise returns null .如果Component直接位于鼠标指针下方,则返回鼠标指针在此Component坐标空间中的位置,否则返回null If the Component is not showing on the screen, this method returns null even if the mouse pointer is above the area where the Component would be displayed.如果Component未显示在屏幕上,即使鼠标指针位于将显示Component的区域上方,此方法也会返回 null。 If the Component is partially or fully obscured by other Component s or native windows, this method returns a non- null value only if the mouse pointer is located above the unobscured part of the Component .如果Component被其他Component或本机窗口部分或完全遮挡,则仅当鼠标指针位于Component的未遮挡部分上方时,此方法才返回非null值。

final Point mousePos = component.getMousePosition();
if (mousePos != null) {
  final int mouseX = mousePos.x;
  final int mouseY = mousePos.y;
  ...
}

... or, if you use a MouseListener , you may see my original comment ... ...或者,如果您使用MouseListener ,您可能会看到我的原始评论...

Try using MouseEvent.getPoint .尝试使用MouseEvent.getPoint

The above will return the mouse point relative to the component to which the listener was bound.以上将返回相对于侦听器绑定到的组件的鼠标点。

public void mouseClicked(final MouseEvent evt) {
  final Point pos = evt.getPoint();
  final int x = pos.x;
  final int y = pos.y;
}

MouseEvent有方法 getX() 和 getY() 返回相对于源组件的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM