运行我的Java应用程序时如何获取当前活动窗口 [英] How to get current active window while running my java application
问题描述
我需要获取当前的活动窗口.我已经使用 KeyboardFocusManager
来获取活动窗口.但我正在活动窗口为空.下面是代码.请提供任何获取当前活动窗口的方法.
I need to get current active window.
I have used KeyboardFocusManager
, for getting active window. But i am getting active window is null.
below is the code.
please provide any way to get current active window.
KeyboardFocusManager currentManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
Window activeWindow = currentManager.getActiveWindow();
推荐答案
这一行代码应该可以工作:
This single line of code should work:
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();
JavaDoc:
返回活动窗口,即使调用线程与活动窗口处于不同上下文中也是如此.只有框架或对话框可以是活动窗口.本机窗口系统可以用特殊装饰(例如突出显示的标题栏)表示活动窗口或其子级.活动窗口始终是焦点窗口,或者是焦点窗口所有者的第一个框架"或对话框".
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!
Note: When your application does not have the focus, this method returns null!
干杯!
这篇关于运行我的Java应用程序时如何获取当前活动窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!