为什么java程序在切换窗口时不继续执行操作?

为什么java程序在切换窗口时不继续执行操作?,java,Java,我正在开发一个Java程序,其中打开一个新的外部程序,然后将鼠标移动到某个位置,然后执行一些键盘操作。我用一个机器人执行所有这些动作,但我注意到,这些动作在外部程序打开后就不起作用了。你知道怎么解决这个问题吗 Robot robot = new Robot(); robot.mouseMove(400, 50); Process process = new ProcessBuilder("PATH/TO/PROGRAM.exe").start();

我正在开发一个Java程序,其中打开一个新的外部程序,然后将鼠标移动到某个位置,然后执行一些键盘操作。我用一个机器人执行所有这些动作,但我注意到,这些动作在外部程序打开后就不起作用了。你知道怎么解决这个问题吗

    Robot robot = new Robot();
    robot.mouseMove(400, 50);
    Process process = new ProcessBuilder("PATH/TO/PROGRAM.exe").start();
            copy(TEXT Which IS COPIED TO CLIPBOARD);
            Thread.sleep(7000);
            robot.mousePress(InputEvent.BUTTON1_DOWN);
            robot.mouseRelease(InputEvent.BUTTON1_DOWN);

copy()是一种将字符串复制到剪贴板的方法。执行程序.exe后,帧焦点似乎会改变,因此操作不会影响程序。 在创建对PROGRAM.exe框架的任何操作之前,您需要获得对该框架的关注

请尝试以下操作:

Robot robot = new Robot();
robot.mouseMove(400, 50);
Process process = new ProcessBuilder("PATH/TO/PROGRAM.exe").start();
//Gaining Focus using Alt+Tab Keys
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_TAB);
robot.delay(10);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_TAB);
// Continuing the rest of program
Thread.sleep(7000);
robot.mousePress(InputEvent.BUTTON1_DOWN);
robot.mouseRelease(InputEvent.BUTTON1_DOWN);

执行程序.exe后,帧焦点似乎会改变,因此操作不会影响程序。 在创建对PROGRAM.exe框架的任何操作之前,您需要获得对该框架的关注

请尝试以下操作:

Robot robot = new Robot();
robot.mouseMove(400, 50);
Process process = new ProcessBuilder("PATH/TO/PROGRAM.exe").start();
//Gaining Focus using Alt+Tab Keys
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_TAB);
robot.delay(10);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_TAB);
// Continuing the rest of program
Thread.sleep(7000);
robot.mousePress(InputEvent.BUTTON1_DOWN);
robot.mouseRelease(InputEvent.BUTTON1_DOWN);

有要共享的代码吗?您在哪个操作系统上运行?Windows 10是我的操作系统有要共享的代码吗?您在哪个操作系统上运行?Windows 10是我的操作系统