可能重复:
how to obtain mouse click coordinates outside my window in Java
我基本上需要找出鼠标指针的位置。我试过这个:
package main;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class MouseListen implements MouseMotionListener{
public void mouseDragged(MouseEvent arg0) {
}
public void mouseMoved(MouseEvent arg0) {
mouseX = arg0.getX();
mouseY = arg0.getY();
}
public int mouseX, mouseY;
}
现在只有在JFrame
窗口内才有效。我怎么能找到JFrame
以外的鼠标的x和y位置?
答案 0 :(得分:3)
MouseInfo.getPointerInfo().getLocation().x;
MouseInfo.getPointerInfo().getLocation().y;
答案 1 :(得分:1)
您想在屏幕上显示鼠标指针的X和Y坐标吗?尝试使用MouseEvent.getXOnScreen()
和MouseEvent.getYOnScreen()
(自Java 1.6起)