找到JFrame外的鼠标位置?

时间:2012-04-05 05:03:47

标签: java swing awt

  

可能重复:
  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位置?

2 个答案:

答案 0 :(得分:3)

MouseInfo.getPointerInfo().getLocation().x;
MouseInfo.getPointerInfo().getLocation().y;

答案 1 :(得分:1)

您想在屏幕上显示鼠标指针的X和Y坐标吗?尝试使用MouseEvent.getXOnScreen()MouseEvent.getYOnScreen()(自Java 1.6起)