gpt4 book ai didi

java - 如何在Java中基于线程停止 Action 事件

转载 作者:行者123 更新时间:2023-12-02 11:56:46 24 4
gpt4 key购买 nike

我有一个程序,当您单击“开始”时,它会移动鼠标光标

单击“开始”后,我创建一个新线程,并且光标开始移动。当我单击“暂停”时,即使变量设置为 false,光标仍然会移动。

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;

public class Main extends JFrame {
private final long serialVersionUID = 1L;
private JPanel contentPane;
private static final int TEN_SECONDS = 10000;
private boolean moving = false;
Thr thr;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);

JButton start = new JButton("Start");
start.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
start.setLocation(163, 20);
start.setSize(146, 53);
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.print("started");
MouseMoveLogic(true);
return;
}
});
contentPane.setLayout(null);
contentPane.add(start);

JButton pause = new JButton("Pause");
pause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.print("paused");
MouseMoveLogic(false);
return;
}
});
pause.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
pause.setBounds(163, 102, 146, 53);
contentPane.add(pause);

JButton quit = new JButton("Quit");
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
quit.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
quit.setBounds(163, 191, 146, 53);
contentPane.add(quit);



}

public void MouseMoveLogic(boolean flag) {
Thr thr = new Thr(moving, flag);
thr.start();
}
}
import java.awt.AWTException;
import java.awt.Robot;
import java.util.Random;

public class Thr extends Thread {
private static Robot robot;
private static final int MAX_Y = 400;
private static final int MAX_X = 400;
private boolean moving;
private boolean flag;

public Thr(boolean moving, boolean flag) {
this.moving = moving;
this.flag = flag;
}

@Override
public void run() {
try {
if (robot == null) {
robot = new Robot();
}

Random random = new Random();

if (!flag && moving) {
moving = flag;
} else if (flag) {
moving = flag;
}

while (moving) {
robot.mouseMove(random.nextInt(MAX_X), random.nextInt(MAX_Y));
robot.delay(2000);
}

if (!moving) {
System.out.println("Trying to stop"); //this is printed when I click 'Pause' so the event is being registered as expected
Thread.currentThread().interrupt();
}

} catch (AWTException e) {
e.printStackTrace();
}
}
}

我无法弄清楚为什么当我单击“暂停”时将移动设置为 false 时鼠标仍继续移动。

任何人都可以看到我哪里出了问题或者是否有更好的方法来解决这个问题?

最佳答案

I cannot figure out why the mouse keeps moving even though moving is set to false when I click 'Pause'.

public void MouseMoveLogic(boolean flag) {
Thr thr = new Thr(moving, flag);
thr.start();
}

您开始一个新话题。所以旧的线程仍在执行。不要启动新线程。

您需要保留对原始线程的引用。然后,您需要在实现 Thread 的类中添加一个类似 setMoving(Boolean moving) 的方法。然后调用 setMoving(...) 方法来启动/停止运动。

if there is a better approach to this problem?

您应该使用 Swing Timer 来安排事件。计时器已经具有您可以调用的停止/启动方法。

阅读 Swing 教程中关于 How to Use Timers 的部分了解更多信息。

一个帮助您入门的简单示例:Jlabel showing both old and new numbers

关于java - 如何在Java中基于线程停止 Action 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47557551/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com