Java 通过摆动按钮停止一根线
我做了一个简单的程序,只跟踪鼠标光标的坐标-它工作得很好,我只想用一个按钮启动/停止它 到目前为止,按钮启动线程良好,我可以安全地停止线程的最佳方式是什么?这是因为我将来可能会添加一个工具来将坐标写入文本文件 我是不是做了一个布尔值,使得线程只在它为真或类似的情况下运行?因为我试图编辑触发器布尔值,但它没有任何影响 目前的代码是: 运行线程的类Java 通过摆动按钮停止一根线,java,multithreading,swing,Java,Multithreading,Swing,我做了一个简单的程序,只跟踪鼠标光标的坐标-它工作得很好,我只想用一个按钮启动/停止它 到目前为止,按钮启动线程良好,我可以安全地停止线程的最佳方式是什么?这是因为我将来可能会添加一个工具来将坐标写入文本文件 我是不是做了一个布尔值,使得线程只在它为真或类似的情况下运行?因为我试图编辑触发器布尔值,但它没有任何影响 目前的代码是: 运行线程的类 public class tester { static int startTime = (int) System.currentTimeMillis
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上不再有StopThreadAPI了。您应该将一个标志设置为volatile,并且您的stop-thread按钮只设置标志变量值
public volatile boolean flag;
public void run() {
while(flag) {
// Get coordinate or whatever you want
}
}
java上不再有StopThreadAPI了。您应该将一个标志设置为volatile,并且您的stop-thread按钮只设置标志变量值
public volatile boolean flag;
public void run() {
while(flag) {
// Get coordinate or whatever you want
}
}
在需要停止时调用shutDown方法
在需要停止时调用shutDown方法
而mouseGrabber.trigger=true将始终为true。您可能希望在mouseGrabber.triggerwhile mouseGrabber.trigger=true始终为true时写入。你可能想在mouseGrabber.trigger的时候写,你能解释一下volatile到底做了什么吗?我现在试试你的解决方案谢谢!如果一个变量设置为volatile,则该变量保证任何线程从内存读写变量都不会缓存。因此,变量值总是更新的值,而不是脏缓存的值。请解释一下volatile实际上做了什么?我现在试试你的解决方案谢谢!如果一个变量设置为volatile,则该变量保证任何线程从内存读写变量都不会缓存。因此,变量值始终是更新值,而不是脏缓存值。嗨,伙计们,我做了我能做的,但那里的按钮对线程没有任何影响。它在程序启动时运行,只有在我关闭它时才停止,而不考虑标志。这里是代码再次感谢所有的帮助嗨,伙计们,我做了我所能做的,但那里的按钮没有任何效果,所以在线程上。它在程序启动时运行,只有在我关闭它时才停止,而不考虑标志。这是代码,再次感谢您的帮助
public void shutDown(){
isShutingDown = true
}