通过摆动按钮停止线程

时间:2012-08-11 21:14:14

标签: java multithreading swing

我制作了一个简单的程序,只跟踪鼠标光标的坐标 - 它工作正常,我想做的就是用按钮开始/停止它。

到目前为止,按钮启动线程很好,我可以安全地停止线程的最佳方法是什么?这是因为我可能会在将来添加一个工具来将坐标写入文本文件。

我是否制作一个布尔值,使线程仅在它为真时运行?因为我试图编辑触发器布尔值,但它没有任何影响。

代码原样:

运行线程的类

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

}

感谢您的帮助,我非常感谢。

2 个答案:

答案 0 :(得分:4)

java上没有stopThread()api了。您应该将标志设置为 volatile ,并且您的停止线程按钮只需设置标志变量值。

public volatile boolean flag;

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

答案 1 :(得分:0)

    public volatile boolean isShutingDown;   



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

   }
}

调用shutDown()方法,当您需要停止时

public void shutDown(){

     isShutingDown = true
    }