2009-09-17 183 views
86

我想模擬Java中的自然鼠標移動(從這裏到像素逐像素)。要做到這一點,我需要知道起始座標。獲取鼠標位置

我已經找到了方法event.getX()和event.getY(),但我需要一個事件...

我怎麼能知道沒有做任何事情(什麼不可見)的位置?

謝謝

回答

178

MouseInfo.getPointerInfo().getLocation()可能是有幫助的。它返回與當前鼠標位置對應的Point對象。

+40

'getPointerInfo()。getLocation()'返回相對於屏幕的位置。如果你想要相對於你的組件的位置(就像MouseListeners給出的那樣),你可以從它減去'yourComponent.getLocationOnScreen()'。 – 2012-01-10 10:43:03

+2

+1'如果鼠標移動速度太快,'Container.getMousePosition()'有時會返回'null',這樣可以避免這個問題。 – 2014-02-07 21:21:06

+0

我們是否可以知道鼠標在自然屏幕上的點擊位置 – Srb 2014-05-30 08:55:24

2

試着看看java.awt.Robot類。它允許您以編程方式移動鼠標。

0

如果您正在使用SWT,您可能需要考慮添加一個MouseMoveListener,如here所述。

+4

但只有我用鼠標執行某些操作(移動,單擊)纔會執行監聽器,對嗎?我在搬運前需要的第一件事就是到起始位置 – 2009-09-17 14:05:19

30
PointerInfo a = MouseInfo.getPointerInfo(); 
Point b = a.getLocation(); 
int x = (int) b.getX(); 
int y = (int) b.getY(); 
System.out.print(y + "jjjjjjjjj"); 
System.out.print(x); 
Robot r = new Robot(); 
r.mouseMove(x, y - 50); 
+13

請加下一次知道一些意見。 – CSchulz 2012-02-06 22:44:16

8

在SWT中,您無需在偵聽器中查看鼠標位置。 Display對象具有方法getCursorLocation()

在香草SWT/JFace中,請致電Display.getCurrent().getCursorLocation()

在RCP應用程序中,請致電PlatformUI.getWorkbench().getDisplay().getCursorLocation()

對於SWT應用程序,最好使用getCursorLocation()而不是其他人提到的MouseInfo.getPointerInfo(),因爲後者在SWT旨在取代的AWT工具包中實現。

0

在我的場景中,我應該根據用鼠標完成的GUI操作,在鼠標位置打開一個對話框。下面的代碼爲我工作:

public Object open() { 
    //create the contents of the dialog 
    createContents(); 
    //setting the shell location based on the curent position 
    //of the mouse 
    PointerInfo a = MouseInfo.getPointerInfo(); 
    Point pt = a.getLocation(); 
    shellEO.setLocation (pt.x, pt.y); 

    //once the contents are created and location is set- 
    //open the dialog 
    shellEO.open(); 
    shellEO.layout(); 
    Display display = getParent().getDisplay(); 
    while (!shellEO.isDisposed()) { 
     if (!display.readAndDispatch()) { 
      display.sleep(); 
     } 
    } 
    return result; 
} 
5
import java.awt.MouseInfo; 
import java.awt.GridLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseEvent; 

import javax.swing.*; 

public class MyClass { 
    public static void main(String[] args) throws InterruptedException{ 
    while(true){ 
     //Thread.sleep(100); 
     System.out.println("(" + MouseInfo.getPointerInfo().getLocation().x + 
       ", " + 
       MouseInfo.getPointerInfo().getLocation().y + ")"); 
    } 
    } 
} 
4
import java.awt.MouseInfo; 
import java.util.concurrent.TimeUnit; 

public class Cords { 

    public static void main(String[] args) throws InterruptedException { 

     //get cords of mouse code, outputs to console every 1/2 second 
     //make sure to import and include the "throws in the main method" 

     while(true == true) 
     { 
     TimeUnit.SECONDS.sleep(1/2); 
     double mouseX = MouseInfo.getPointerInfo().getLocation().getX(); 
     double mouseY = MouseInfo.getPointerInfo().getLocation().getY(); 
     System.out.println("X:" + mouseX); 
     System.out.println("Y:" + mouseY); 
     //make sure to import 
     } 

    } 

} 
0

我做這樣的事情讓鼠標座標使用機器人,我在一些比賽,我發展的進一步使用這些座標:

public class ForMouseOnly { 
    public static void main(String[] args) throws InterruptedException { 
     int x = MouseInfo.getPointerInfo().getLocation().x; 
     int y = MouseInfo.getPointerInfo().getLocation().y; 
     while (true) { 

      if (x != MouseInfo.getPointerInfo().getLocation().x || y != MouseInfo.getPointerInfo().getLocation().y) { 
       System.out.println("(" + MouseInfo.getPointerInfo().getLocation().x + ", " 
         + MouseInfo.getPointerInfo().getLocation().y + ")"); 
       x = MouseInfo.getPointerInfo().getLocation().x; 
       y = MouseInfo.getPointerInfo().getLocation().y; 
      } 
     } 
    } 
}