簡體   English   中英

通過擺動按鈕停止線程

[英]Stopping a thread by a swing button

我編寫了一個簡單的程序,該程序只跟蹤鼠標光標的坐標-它工作正常,我要做的就是用按鈕啟動/停止它。

到目前為止,按鈕可以很好地啟動線程,可以安全地停止線程的最佳方法是什么? 這是因為將來我可能會添加一種將坐標寫入文本文件的功能。

我是否要使布爾值僅在線程為真或類似情況時才運行? 因為我試圖編輯觸發器布爾值,但是它至今都沒有影響。

代碼如下:

運行線程的類

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;
}

}

謝謝您的幫助,我真的很感激。

Java上不再有stopThread()api。 您應該將標志設置為volatile ,並且停止線程按鈕只需設置標志變量值即可。

public volatile boolean flag;

public void run() {
   while(flag) {
       // Get coordinate or whatever you want      
   }
}
    public volatile boolean isShutingDown;   



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

   }
}

需要停止時,調用shutdownDown()方法

public void shutDown(){

     isShutingDown = true
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM