gpt4 book ai didi

java - 有没有办法在机器人类(class)开始后停止它

转载 作者:行者123 更新时间:2023-12-01 12:47:01 27 4
gpt4 key购买 nike

有没有办法在 Java Robot 启动后停止执行?我有一个模拟鼠标左键单击的程序,但我还有一个名为 STOP 的未使用的 JButton,它应该停止我开始使用 Robot 类的单击。我注意到 Robot 比 Thread 类要难一些。有什么想法吗?

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.util.Random;

public class Click{
Robot robot = new Robot();

private void leftClick(){
int no = new Random().nextInt(6) + 1;
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(50 * no);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(220 * no);
}

public Click() throws AWTException{
while (true) {
leftClick();
System.out.println("Click");
}
}
}

在 GUI 类中,我有一个看起来像这样的 JButton:

private JButton getBtnStart() {
if (btnStart == null) {
btnStart = new JButton("Start");
btnStart.setBackground(Color.WHITE);
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
c = new Click();
} catch (AWTException e1) {
e1.printStackTrace();
}
}
});
}
return btnStart;
}

最佳答案

有几个问题。构造函数中的无条件循环是不允许的。除此之外,您还阻止了 EDT,所以我想知道这是否可行。

我什至不清楚你想如何停止这种点击,因为在机器人工作时你几乎无法做任何事情。

但是,根据您目前提供的信息,以下是如何解决此问题的一个草图:

import java.awt.AWTException;
import java.awt.GridLayout;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class RobotControlTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}

private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final RobotControl robotControl = new RobotControl();

f.getContentPane().setLayout(new GridLayout(1,2));

final JButton startButton = new JButton("Start");
f.getContentPane().add(startButton);

final JButton stopButton = new JButton("Stop");
stopButton.setEnabled(false);
f.getContentPane().add(stopButton);

startButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
startButton.setEnabled(false);
stopButton.setEnabled(true);
robotControl.startClicking();
}
});

stopButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
robotControl.stopClicking();
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
});

f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}

class RobotControl
{
private final Random random = new Random();
private final Robot robot;
private volatile boolean running = false;

RobotControl()
{
try
{
robot = new Robot();
}
catch (AWTException e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
}

void startClicking()
{
Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
running = true;
performClicks();
}
});
thread.start();
}

void stopClicking()
{
System.out.println("Stopping");
running = false;
}

private void performClicks()
{
System.out.println("Starting");
while (running)
{
leftClick();
System.out.println("Clicked");
}
}

private void leftClick()
{
int no = random.nextInt(6) + 1;
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(50 * no);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(220 * no);
}
}
<小时/>

EDIT based on the comment:

当然可以检测鼠标移动,并在每次鼠标移动后暂停点击一段时间。然而,我不喜欢事情的发展方向。答案的第一部分只是一个快速的草图,以说明当 reading a bit about threads 时您可以自己开发的想法。 。对此草图的任何扩展都是不合适的。我的直觉是,这将以一系列进一步的要求结束,这些要求可能以某种方式被混入原始答案中,但应该从一开始就说明

但我看到你的问题:“开发一个可以满足我需要的程序”不是一个有效的问题。

但至少要回答“如何检测鼠标移动?”的“隐含”问题:

您可以使用MouseInfo.getPointerInfo().getLocation()获取鼠标位置,并定期检查该点是否与前一个点不同。

拼凑到原始程序中:

import java.awt.AWTException;
import java.awt.GridLayout;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class ExtendedRobotControlTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}

private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final RobotControl robotControl = new RobotControl();

f.getContentPane().setLayout(new GridLayout(1,2));

final JButton startButton = new JButton("Start");
f.getContentPane().add(startButton);

final JButton stopButton = new JButton("Stop");
stopButton.setEnabled(false);
f.getContentPane().add(stopButton);

startButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
startButton.setEnabled(false);
stopButton.setEnabled(true);
robotControl.startControl();
}
});

stopButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
robotControl.stopControl();
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
});

f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}

class RobotControl
{
private final Random random = new Random();
private final Robot robot;

private Thread clickingThread;
private Thread observingThread;

private long lastMovementMillis = -1;
private Point lastMousePosition = null;

private volatile boolean running = false;

RobotControl()
{
try
{
robot = new Robot();
}
catch (AWTException e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
}

void startObserver()
{
observingThread = new Thread(new Runnable(){

@Override
public void run()
{
observeMovement();
}
});
observingThread.start();
}

private void observeMovement()
{
while (running)
{
Point p = MouseInfo.getPointerInfo().getLocation();
if (!p.equals(lastMousePosition))
{
System.out.println("Movement detected");
lastMovementMillis = System.currentTimeMillis();
lastMousePosition = p;
}
try
{
Thread.sleep(50);
}
catch (InterruptedException e)
{
e.printStackTrace();
Thread.currentThread().interrupt();
return;
}
}
}


void startControl()
{
stopControl();
System.out.println("Starting");
lastMovementMillis = System.currentTimeMillis();
lastMousePosition = MouseInfo.getPointerInfo().getLocation();
running = true;
startClicking();
startObserver();
}

private void startClicking()
{
clickingThread = new Thread(new Runnable()
{
@Override
public void run()
{
performClicks();
}
});
clickingThread.start();
}

void stopControl()
{
if (running)
{
System.out.println("Stopping");
running = false;
try
{
clickingThread.join(5000);
observingThread.join(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}

private void performClicks()
{
System.out.println("Starting");
while (running)
{
long t = System.currentTimeMillis();
if (t > lastMovementMillis + 1000)
{
leftClick();
System.out.println("Clicked");
}
else
{
System.out.println("Waiting before clicks...");
try
{
Thread.sleep(50);
}
catch (InterruptedException e)
{
e.printStackTrace();
Thread.currentThread().interrupt();
return;
}
}
}
}

private void leftClick()
{
int no = random.nextInt(6) + 1;
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(50 * no);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(220 * no);
}
}

关于java - 有没有办法在机器人类(class)开始后停止它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24596936/

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