如何将重点放在Windows 7上

时间:2014-02-26 23:21:10

标签: java swing intellij-idea

我正在尝试将焦点从一个IntelliJ实例转移到另一个实例。即使窗口显示在所有内容之上并且光标开始闪烁,焦点实际上在前一个窗口中,并且图标在任务栏中闪烁。

在一个进程的帧之间切换很好,但切换到另一个进程是个问题。

Windows 7 x64,jdk1.7.0_51

JFrame frame = WindowManager.getInstance().getFrame(project);

//the only reliable way I found to bring it to the top
boolean aot = frame.isAlwaysOnTop();
frame.setAlwaysOnTop(true);
frame.setAlwaysOnTop(aot);

int frameState = frame.getExtendedState();
if ((frameState & Frame.ICONIFIED) == Frame.ICONIFIED) {
    // restore the frame if it is minimized
    frame.setExtendedState(frameState ^ Frame.ICONIFIED);
}
frame.toFront();
frame.requestFocus();
//frame.requestFocusInWindow(); same behaviour as requestFocus

我也尝试过机器人,并在其他问题中建议创建新的临时框架而没有运气。

2 个答案:

答案 0 :(得分:1)

似乎我以前错误地使用过机器人黑客,因为现在它似乎工作正常:

        try {
            //remember the last location of mouse
            final Point oldMouseLocation = MouseInfo.getPointerInfo().getLocation();

            //simulate a mouse click on title bar of window
            Robot robot = new Robot();
            robot.mouseMove(frame.getX(), frame.getY());
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

            //move mouse to old location
            robot.mouseMove((int) oldMouseLocation.getX(), (int) oldMouseLocation.getY());
        } catch (Exception ex) {
            //just ignore exception, or you can handle it as you want
        } finally {
            frame.setAlwaysOnTop(false);
        }

来源:https://stackoverflow.com/a/7404378/685796

答案 1 :(得分:0)

JFrame.setVisible(true)通常适合摆动。