如何获得相对于挥杆窗口的鼠标单击位置 [英] How to get location of a mouse click relative to a swing window
本文介绍了如何获得相对于挥杆窗口的鼠标单击位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我在Java Swing JFrame中。我点击我的鼠标。我想在GUI中单击鼠标的位置。在java中,行
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
int mouseX = MouseInfo.getPointerInfo().getLocation.x;
似乎在整个屏幕上给出了鼠标的位置。我如何得到它相对于GUI的位置?
Seems to give the location of the mouse on the entire screen. How would I get it's location relative to the GUI?
推荐答案
来自 MouseListener
可以执行的方法:
From MouseListener
methods you can do:
@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.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
参考:
- How to Write a Mouse Listener
这篇关于如何获得相对于挥杆窗口的鼠标单击位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文