gpt4 book ai didi

java - 如何在运行 java 应用程序时获取当前 Activity 窗口

转载 作者:行者123 更新时间:2023-12-02 09:13:54 24 4
gpt4 key购买 nike

我需要获取当前 Activity 窗口。我使用了KeyboardFocusManager来获取 Activity 窗口。但我发现 Activity 窗口为空。下面是代码。请提供任何获取当前 Activity 窗口的方法。

KeyboardFocusManager currentManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();  
Window activeWindow = currentManager.getActiveWindow();

最佳答案

这行代码应该可以工作:

Window activeWindow = javax.swing.FocusManager.getCurrentManager().getActiveWindow();

来自 JavaDoc:

Returns the active Window, if the active Window is in the same context as the calling thread. Only a Frame or a Dialog can be the active Window. The native windowing system may denote the active Window or its children with special decorations, such as a highlighted title bar. The active Window is always either the focused Window, or the first Frame or Dialog that is an owner of the focused Window.

要获取GlobalActiveWindow,请调用:

 javax.swing.FocusManager.getCurrentManager().getGlobalActiveWindow();

Java文档:

Returns the active Window, even if the calling thread is in a different context than the active Window. Only a Frame or a Dialog can be the active Window. The native windowing system may denote the active Window or its children with special decorations, such as a highlighted title bar. The active Window is always either the focused Window, or the first Frame or Dialog that is an owner of the focused Window.

注意:当您的应用程序没有焦点时,此方法返回 null!

干杯!

关于java - 如何在运行 java 应用程序时获取当前 Activity 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34992027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com