如何获得相对于挥杆窗口的鼠标单击位置

时间:2022-11-28 20:47:40

Say I'm in a Java Swing JFrame. I click my mouse. I want to get the location of the mouse click within the GUI. In java, the line

说我在Java Swing JFrame中。我点击我的鼠标。我想在GUI中获取鼠标单击的位置。在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的位置?

4 个解决方案

#1


16  

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.addMouseListener(new MouseListener() {
    @Override
    public void mouseClicked(MouseEvent e) {
    }
});

Reference:

参考:

#2


7  

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. 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. If the Component is partially or fully obscured by other Components 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的坐标空间中鼠标指针的位置,否则返回null。如果屏幕上未显示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;
}

#3


7  

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

#4


3  

MouseEvent has methods getX() and getY() that return the position relative to the source component.

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

#1


16  

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.addMouseListener(new MouseListener() {
    @Override
    public void mouseClicked(MouseEvent e) {
    }
});

Reference:

参考:

#2


7  

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. 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. If the Component is partially or fully obscured by other Components 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的坐标空间中鼠标指针的位置,否则返回null。如果屏幕上未显示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;
}

#3


7  

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

#4


3  

MouseEvent has methods getX() and getY() that return the position relative to the source component.

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