无法从事件调度程序线程调用方法
Cannot call method from the event dispatcher thread
我正在编写一个记录用户鼠标移动和点击的程序,并使用 Robot
class.
播放它们
我运行遇到这个错误:
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 代码在这里:
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();
}
}
您必须调用方法:
Thread t = new Thread();
t.start();
而不是
t.run();
否则将不会启动新线程,您将在同一个线程
中执行 运行 方法
我正在编写一个记录用户鼠标移动和点击的程序,并使用 Robot
class.
我运行遇到这个错误:
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 代码在这里:
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();
}
}
您必须调用方法:
Thread t = new Thread();
t.start();
而不是
t.run();
否则将不会启动新线程,您将在同一个线程
中执行 运行 方法