如何在Java中设置鼠标的位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2941324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set the position of the mouse in Java?
提问by dreadwail
I'm doing some Swing GUI work with Java, and I think my question is fairly straightforward; How does one set the position of the mouse?
我正在用 Java 做一些 Swing GUI 工作,我认为我的问题很简单;如何设置鼠标的位置?
采纳答案by Matt
You need to use Robot
你需要使用机器人
This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.
Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform's native input queue. For example,
Robot.mouseMove
will actually move the mouse cursor instead of just generating mouse move events...
此类用于生成本地系统输入事件,以用于测试自动化、自运行演示以及需要控制鼠标和键盘的其他应用程序。Robot 的主要目的是促进 Java 平台实现的自动化测试。
使用该类生成输入事件与将事件发布到 AWT 事件队列或 AWT 组件的不同之处在于,事件是在平台的本机输入队列中生成的。例如,
Robot.mouseMove
将实际移动鼠标光标而不是仅生成鼠标移动事件...
回答by OscarRyz
回答by Daniel
As others have said, this can be achieved using Robot.mouseMove(x,y)
. However this solution has a downfall when working in a multi-monitor situation, as the robot works with the coordinate system of the primary screen, unless you specify otherwise.
正如其他人所说,这可以使用Robot.mouseMove(x,y)
. 然而,在多显示器情况下工作时,此解决方案有缺陷,因为机器人使用主屏幕的坐标系工作,除非您另行指定。
Here is a solution that allows you to pass any point based global screen coordinates:
这是一个允许您传递基于任何点的全局屏幕坐标的解决方案:
public void moveMouse(Point p) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
// Search the devices for the one that draws the specified point.
for (GraphicsDevice device: gs) {
GraphicsConfiguration[] configurations =
device.getConfigurations();
for (GraphicsConfiguration config: configurations) {
Rectangle bounds = config.getBounds();
if(bounds.contains(p)) {
// Set point to screen coordinates.
Point b = bounds.getLocation();
Point s = new Point(p.x - b.x, p.y - b.y);
try {
Robot r = new Robot(device);
r.mouseMove(s.x, s.y);
} catch (AWTException e) {
e.printStackTrace();
}
return;
}
}
}
// Couldn't move to the point, it may be off screen.
return;
}
回答by noob_Coder
The code itself is the following:
代码本身如下:
char escCode = 0x1B;
System.out.print(String.format("%c[%d;%df",escCode,row,column));
This code is incomplete by itself, so I recommend placing it in a method and calling it something like 'positionCursor(int row, int column)'.
这段代码本身是不完整的,所以我建议把它放在一个方法中,并像'positionCursor(int row, int column)'这样的东西调用它。
Here is the code in full (method and code):
这是完整的代码(方法和代码):
void positionCursor(int row, int column) {
char escCode = 0x1B;
System.out.print(String.format("%c[%d;%df",escCode,row,column));
}