我正在编写一个记录用户鼠标移动和点击的程序,并使用Robot
类播放它们。
我遇到了这个错误:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException: Cannot call method from the event dispatcher thread
我已经阅读了有关EDT的所有信息,人们一直在提及,必须在另一个线程中运行它才能退出EDT。
我的问题是:即使我使用了一个新主题,为什么我的代码也不起作用?
以下是代码:
void doAction(Robot robert) {
int x = ((MouseEvent) event).getXOnScreen();
int y = ((MouseEvent) event).getYOnScreen();
Thread safe = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(SwingUtilities.isEventDispatchThread());
MouseEvent m = (MouseEvent) this.event; // event is the recording of the click
robert.mouseMove(x, y); // error traces back to here
leftClick(robert);
}
});
safe.run();
}
System.out.println(SwingUtilities.isEventDispatchThread());
打印真实
整个班级代码在这里:
class RoboMouseClick extends RoboAction {
AWTEvent event;
public RoboMouseClick(String mouse, int MOUSE_MOVE, AWTEvent event,
long timeStamp) {
super(mouse, MOUSE_MOVE, timeStamp);
this.event = event;
}
private void leftClick(Robot robot)
{
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(200);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(200);
}
void doAction(Robot robert) {
int x = ((MouseEvent) event).getXOnScreen();
int y = ((MouseEvent) event).getYOnScreen();
Thread safe = new Thread(() -> {
System.out.println(SwingUtilities.isEventDispatchThread());
MouseEvent m = (MouseEvent) event;
robert.mouseMove(x, y);
leftClick(robert);
});
safe.run();
}
}
答案 0 :(得分:3)
您必须调用方法:
const PureComponent = ({url}) => (
<div>
<a href={url}>{children}</a> // <--- This doesn't work
</div>
);
<PureComponent url="http://www/google.ca">Google Canada</PureComponent>
// I want to render this:
<a href="http://www.google.ca">Google Canada</a>
而不是
Thread t = new Thread();
t.start();
否则不会启动新线程,您将在同一个线程中执行run方法