假设我在 Java Swing JFrame 中。我点击我的鼠标。我想获取鼠标点击的位置在 GUI 中。在java中,行
int mouseX = MouseInfo.getPointerInfo().getLocation.x;
似乎给出了鼠标在整个屏幕上的位置。我如何获得它相对于 GUI 的位置?
最佳答案
来自 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
}
只需通过以下方式将其添加到您的 Component
中:
component.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
引用:
关于java - 如何获取鼠标单击相对于 Swing 窗口的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12396066/