2012-07-20 189 views
1

我想跟蹤鼠標光標的位置,因此需要使用一個線程來繼續跟蹤它。我的線程只運行一次。我猜測我錯過了一些東西。爲什麼線程只運行一次?

代碼:

mousePosition類

import java.awt.MouseInfo; 
import java.awt.Point; 
import java.util.Timer; 

public class mousePosition implements Runnable 
{ 
    static Point mouseLocation = MouseInfo.getPointerInfo().getLocation(); 
    static Timer t = new Timer(); 
    int x,y = 0; 


    public void run() 
    { 
     try 
     { 
       x = mouseLocation.x; 
       y = mouseLocation.y; 

       System.out.println("X:"+x+" Y:"+y+" at time "+System.currentTimeMillis()); 
     } 

     catch(Exception e) 
     { 
      System.out.println("Exception caught : "+e); 
     } 

    } 



} 

主類

public class threadRunner 
{ 
    public static void main(String[] args) 
    { 
     Thread t1 = new Thread(new mousePosition()); 

     t1.start(); 

    } 

} 

感謝您的任何幫助。我知道這個問題以前曾被問過,但我仍然在努力使其工作。

+3

由於沒有循環,並且您不使用計時器,所以'run'方法只運行一次是有道理的。 – assylias 2012-07-20 11:42:00

+0

你爲什麼不在你的線程中使用你的計時器?線程的'run()'方法將在執行代碼並結束您的線程後始終返回! – 2012-07-20 11:45:14

+0

你通常在run方法中有一些while語句,以保持線程正常運行,而且經常是真的 – 2012-07-20 11:46:40

回答

6

線程的run()方法將只運行一次。雖然我敢肯定,在一個線程這個while循環是昂貴的,在計算成本上講

public void run() { 
    while(true) { // Or with a stop condition 
    try { 
      x = mouseLocation.x; 
      y = mouseLocation.y; 

      System.out.println("X:"+x+" Y:"+y+" at time "+System.currentTimeMillis()); 

    } catch(Exception e) { 
     System.out.println("Exception caught : "+e); 
    } 
    } 
} 

,並且存在使用Observer design pattern一個更好的做法:它會跑得更如果你做了這樣的事情。這個良好實踐的實施和示例恰恰就是MouseListener

2

當你start()一個新的Thread,唯一發生的是執行Thread s run()方法。一旦run()方法完成,Thread死亡。

要不斷地檢查鼠標的位置,你應該開始某種MouseListener相反,有一個很好的教程here

要了解有關線索的更多信息,我建議您看看投擲Java Tutorials on the subject

+0

啊好吧,這是有道理的..所以明智地循環嘗試代碼在運行?因爲當我這樣做時,它給了我相同鼠標位置的答案,這也是合理的。我認爲它是一個問題,在哪裏放置循環 – alexeidebono 2012-07-20 11:45:56

+0

我會看看這些鏈接謝謝! – alexeidebono 2012-07-20 11:47:17

+0

@alexeidebono:由於您只在創建線程時檢查鼠標位置,因此循環在此處不會執行太多操作。查看一些有用的閱讀我的鏈接。 – Keppil 2012-07-20 11:47:31

0

做到這一點:

public void run() 
    { 
     try 
     {  
       boolean mouseTracked = false; 
       while(!mouseTracked){ 
        x = mouseLocation.x; 
        y = mouseLocation.y; 

       System.out.println("X:"+x+" Y:"+y+" at time "+System.currentTimeMillis()); 
       mouseTracked = true; //do this when mouse tracked.. 
     } 
} 
     catch(Exception e) 
     { 
      System.out.println("Exception caught : "+e); 
     } 

    } 

這將繼續追蹤鼠標,直到你設置mouseTracked爲true,設置mouseTracked到真實的,你會在比賽中狀態

0

做改變你的類像這樣

import java.awt.MouseInfo; 
import java.awt.Point; 
import java.util.Timer; 

public class MousePosition implements Runnable 
{ 
static Point mouseLocation = null; 
static Timer t = new Timer(); 
int x,y = 0; 


public void run() 
{ 
    while(true){ 
    try 
    { 
      mouseLocation= MouseInfo.getPointerInfo().getLocation(); 

      x = mouseLocation.x; 
      y = mouseLocation.y; 

      System.out.println("X:"+x+" Y:"+y+" at time "+System.currentTimeMillis()); 
    } 

    catch(Exception e) 
    { 
     System.out.println("Exception caught : "+e); 
    } 
    } 
} 
} 
2

這裏讀鼠標位置每隔50ms

01簡單的方法