代码之家  ›  专栏  ›  技术社区  ›  AlexR

如何在Java中激活窗口?

  •  5
  • AlexR  · 技术社区  · 15 年前

    我想用程序激活我的Swing应用程序。我是说我想写一些代码 JFrame 可见并聚焦(窗口标题应突出显示)。 我试着用 requestFocus() A.requestFocus() 它变得像我想要的那样活跃。如果应用程序只有一个窗口或两个窗口都不可见,则不会发生这种情况。

    我找到了两个解决办法。

    1. 呼叫 A.setAlwaysOnTop(true) . 这使得窗口A位于其他窗口的顶部。但目前还没有焦点。使用 java.awt.Robot (mouseMove,mousePress,mouseRelease)单击窗口a的标题。 现在打电话 A.setAlwaysOnTop(false) 我实现了代码,它也能工作,但看起来是一个丑陋的解决方法。

    有“正确”的解决方案吗?

    4 回复  |  直到 12 年前
        1
  •  1
  •   Community Mohan Dere    9 年前

    我相信 this 波斯特应该会帮你的。

        2
  •  5
  •   Brad Mace Mike King    15 年前
    frame.setState(Frame.NORMAL); // restores minimized windows
    frame.toFront(); // brings to front without needing to setAlwaysOnTop
    frame.requestFocus();
    

    想知道的所有细节,请看本页: http://www.developer.com/java/other/article.php/3502181/Window-Focus-and-State-in-Java.htm

        3
  •  1
  •   Michael Goldshteyn    15 年前

    这应该做到:

    frame.setSelected(true);
    

    你可能想把它放在try/catch块里。。。

    如果这在你使用的操作系统上不起作用,还有两种可能性:

    frame.setAlwaysOnTop(true);
    frame.setAlwaysOnTop(false);
    

    frame.setVisible(true);
    frame.setVisible(true); // Yes you need this second one
    
        4
  •  1
  •   Bo Persson Touseef    9 年前

    我在同一条船上-上面的都没用。

    “我的”解决方案如下:

    thisFrame.getWindowListeners()[0].windowActivated(
         new WindowEvent(
                  thisFrame,
                  WindowEvent.WINDOW_ACTIVATED
         )
    );
    schedulesTable.requestFocus();
    
    thisFrame = the window to get activated
    
    schedulesTable = my component in the window I wanted to get focus for
    
        5
  •  1
  •   Dmitriy Potapov    7 年前

    我找到了解决问题的方法:

    //frame - JFrame
    frame.setExtendedState(JFrame.ICONIFIED);
    frame.setExtendedState(JFrame.NORMAL);
    frame.toFront();
    frame.requestFocus();