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

}

谢谢你的帮助,我真的很感激。

4

2 回答 2

4

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

public volatile boolean flag;

public void run() {
   while(flag) {
       // Get coordinate or whatever you want      
   }
}
于 2012-08-11T21:20:38.567 回答
0
    public volatile boolean isShutingDown;   



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

   }
}

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

public void shutDown(){

     isShutingDown = true
    }
于 2012-08-12T06:09:57.263 回答