因为Robot 正在生成本机事件,该事件将(最终)进入事件队列以供 EDT 处理。
这意味着如果你尝试做类似...
removeMouseListener(...);
Robot.mouseMove(...);
addMouseListener(...);
它基本上不会有任何效果,因为鼠标监听器的移除和附加发生在事件处理的同一周期,这意味着机器人引发的鼠标事件不会被处理(或将出现)稍后在队列中)...
相反,您需要提出某种可以检测到的标志,然后忽略下一个传入事件...
if (!ignoreMouseMove) {
ignoreMouseMove = true;
// Do your normal processing...
robot.mouseMove(...);
} else {
ignoreMouseMove = false;
}
下面的基本示例检测鼠标移动到中心的距离并更新一个简单的position 变量(它基本上充当罗盘点)。这有助于说明运动,但更重要的是,我们正在打破事件循环......
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Robot;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestMouseMove {
public static void main(String[] args) {
new TestMouseMove();
}
public TestMouseMove() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Robot bot;
private int position = 0;
public TestPane() {
try {
bot = new Robot();
MouseAdapter ma = new MouseAdapter() {
boolean ignoreMouseMove = false;
@Override
public void mouseMoved(MouseEvent e) {
if (!ignoreMouseMove) {
ignoreMouseMove = true;
int x = getLocationOnScreen().x + (getWidth() / 2);
int y = getLocationOnScreen().y + (getHeight() / 2);
int distanceFromCenter = e.getPoint().x - (getWidth() / 2);
position += distanceFromCenter;
if (position < 0) {
position = 360 - position;
} else if (position > 360) {
position -= 360;
}
repaint();
bot.mouseMove(x, y);
} else {
ignoreMouseMove = false;
}
}
@Override
public void mouseClicked(MouseEvent e) {
System.exit(0);
}
};
addMouseListener(ma);
addMouseMotionListener(ma);
} catch (AWTException ex) {
ex.printStackTrace();;
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
FontMetrics fm = g2d.getFontMetrics();
int x = getWidth() / 2;
int y = getHeight() / 2;
int amount = position;
while (x > 0) {
if (amount == position) {
g2d.drawLine(x, y, x, y - 40);
} else {
g2d.drawLine(x, y, x, y - 20);
}
String text = Integer.toString(amount);
g2d.drawString(text, x - (fm.stringWidth(text) / 2), y + fm.getHeight());
x -= 20;
amount--;
if (amount < 0) {
amount = 360 + amount;
}
}
amount = position + 1;
x = (getWidth() / 2) + 20;
while (x < getWidth()) {
g2d.drawLine(x, y, x, y - 20);
if (position > 360) {
position = 360 - position;
}
String text = Integer.toString(amount);
g2d.drawString(text, x - (fm.stringWidth(text) / 2), y + fm.getHeight());
x += 20;
amount++;
}
g2d.dispose();
}
}
}