我正在尝试编写一个程序,它可以根据时间将鼠标光标带到特定的坐标,而不管用户是谁。我用Robot写了一个简单的代码,但遇到了一个问题...我有两个显示器,光标的移动不正确,这取决于它是在哪个显示器上,请告诉我如何解决这个问题。
下面的代码是我试图创建的.
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] graphicsDevices = graphicsEnvironment.getScreenDevices();
for(int i=0; i < graphicsDevices.length; i++)
{
System.out.println(graphicsDevices[i]);
}
try {
//Robot robot = new Robot(MouseInfo.getPointerInfo().getDevice());
Robot robot = new Robot();
while(true)
{
robot.mouseMove(-1640, -3);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(10000);
}
} catch (Exception e) {
e.printStackTrace();
}发布于 2017-10-19 08:47:52
你应该先得到解决方案,然后再从那里开始。你正在做ABSOLUTE动作,它们在不同的设置中会有不同的效果。
您应该使用以下代码
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();在那里,您可以:
Robot robot = new Robot();
robot.mouseMove (width-10, height+3);因此,您可以将相对移到显示器的规格上。我希望我能帮上忙。
https://stackoverflow.com/questions/46820255
复制相似问题