2012-08-11 76 views
2

我做了一個簡單的程序,它只是跟蹤鼠標光標的座標 - 它工作正常,我想要做的就是用按鈕啓動/停止它。通過擺動按鈕停止線程

到目前爲止,按鈕啓動線程,我可以安全地停止線程的最佳方式是什麼?這是因爲我可能會添加一個工具來將座標寫入文本文件中。

難道我做了一個布爾值,只有當它是真的或類似的東西時才讓線程運行?因爲我試圖編輯觸發器布爾值,但它並沒有影響到什麼。

,因爲它的代碼是:

類運行的線程

public class tester { 

static int startTime = (int) System.currentTimeMillis(); 
static boolean trigger = false; 
static JLabel label = new JLabel(); 
static JLabel status = new JLabel(); 
static mouseLocate msl = new mouseLocate(); 
static JButton startButton = new JButton("Begin tracking"); 
static JButton stopButton = new JButton("Stop Tracker"); 
static Thread myThread = new Thread(new mouseLocate()); 

public static void main(String[] args) { 
    JFrame frame = new JFrame("Mouse Grabber"); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(500, 100); 
    JPanel panel = new JPanel(); 
    frame.add(panel); 


    panel.add(startButton); 
    startButton.addActionListener(new startAction()); 

    panel.add(label); 
    panel.add(status); 
} 

static class startAction implements ActionListener { 

    public void actionPerformed(ActionEvent e) { 

     try { 

      if (trigger == false) { 
       trigger = true; 
       msl.setTrigger(trigger); 
       //label.setText("Trigger Active" + msl.isTrigger()); 
       startButton.setText("Continue Tracking"); 
      } else if (trigger == true) { 
       trigger = false; 
       //msl.setTrigger(trigger); 
       label.setText("Trigger Inactive"); 
       startButton.setText("Stop Tracking"); 
      } 
     } catch (Exception exp) { 
      System.out.println("EXCEPTION CAUGHT " + e); 
     } 



     //RUN 
     myThread.start(); 


    } 
} 

鼠標定位類:

public class mouseLocate implements Runnable { 

private boolean trigger = false; 
private int startTime = (int) System.currentTimeMillis(); 
static String status = ""; 

public void run() { 
    int x, y; 
    while (mouseGrabber.trigger = true) { 

     try { 
      PointerInfo mouseLocation = MouseInfo.getPointerInfo(); 
      x = mouseLocation.getLocation().x; 
      y = mouseLocation.getLocation().y; 
      System.out.println("X:" + x + " Y:" + y + "  Elapsed time: " 
        + (((int) System.currentTimeMillis() - startTime)/100)); 

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

} 

public int getStartTime() { 
    return startTime; 
} 

public void setStartTime(int startTime) { 
    this.startTime = startTime; 
} 

public boolean isTrigger() { 
    return trigger; 
} 

public void setTrigger(boolean trigger) { 
    this.trigger = trigger; 
} 

public static String getStatus() { 
    return status; 
} 

public static void setStatus(String status) { 
    mouseLocate.status = status; 
} 

}

謝謝你的任何幫助,我真的欣賞它。

+0

'而(mouseGrabber.trigger =真)'永遠是正確的。你可能想寫'while(mouseGrabber.trigger)' – nkr 2012-08-11 21:31:47

回答

4

Java上沒有stopThread()api了。您應該設置一個標誌爲易失性,並且您的停止線程按鈕只需設置標誌變量值。

public volatile boolean flag; 

public void run() { 
    while(flag) { 
     // Get coordinate or whatever you want  
    } 
} 
+0

你能解釋一下實際的volatile嗎?生病嘗試你的解決方案,謝謝你! – alexeidebono 2012-08-12 09:37:48

+0

如果一個變量設置爲volatile,保證任何線程訪問變量讀/寫從/到內存不緩存。因此,變量值總是更新值,而不是髒(緩存)值。 – 2012-08-12 09:47:35

0
public volatile boolean isShutingDown; 



public void run() { 
    while(!isShutingDown) { 

    } 
} 

調用shutdown()方法,當你需要停止

public void shutDown(){ 

    isShutingDown = true 
    } 
+0

嗨,大家好,我盡我所能,但那裏的按鈕沒有任何影響線程上的東西。它在程序啓動時運行,只有在關閉它時纔會停止,而不管標誌是什麼。 這裏是代碼[鏈接](http://pastebin.com/AihWCLsJ) 再次感謝您的任何幫助 – alexeidebono 2012-08-18 19:11:54