【问题标题】:Get mouse coordinates at any point on the screen获取屏幕上任意点的鼠标坐标
【发布时间】:2016-03-19 04:48:03
【问题描述】:

我试图在每次鼠标在屏幕上的任意点移动时获取鼠标的坐标,然后记录坐标,但我对如何做有点迷茫。

目前,我正在尝试使用 MouseListener,那么有没有办法在整个屏幕上制作一个透明、可点击并能够捕获鼠标事件的叠加层?

感谢您的帮助。

【问题讨论】:

  • 当鼠标在 Java 创建的窗口之外时,没有侦听器会自动执行此操作,您需要使用 PointInfo,exampleexampleexample
  • example

标签: java mouseevent coordinates mouse overlay


【解决方案1】:

将 MouseMotionListener 与 mouseDragged 方法一起使用,例如:

public void mouseDragged(MouseEvent e) {
    Graphics g = this.getGraphics();
    int x = e.getX(), y = e.getY(); // you have the coordinates
  // if you want to draw a line for example between 2 mouse pos:
    g.drawLine(lastX,lastY,x,y);
    lastX =x;
    lastY =y;
}

【讨论】:

    【解决方案2】:

    您需要遵循以下步骤,

    第 1 步: implements MouseMotionListener 到您的班级的接口,

    步骤:2:覆盖它的 2 方法

    1. 鼠标拖拽
    2. 鼠标移动

    步骤:3:将以下代码放入mouseMoved(...),

    在这里,仅出于解释目的,我采用了 2-label 这将 显示当前鼠标的位置。

    @Override
        public void mouseMoved(MouseEvent e) {
            String xvalue = new Integer(e.getX()).toString();
            String yvalue = new Integer(e.getY()).toString();
            xlable.setText(xvalue); // here , you can write it into you log
            ylable.setText(yvalue); // here , you can write it into you log
        }
    

    我努力的成果,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      • 1970-01-01
      • 2022-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多