我在AWT中看到了一种方法:java.awt.Window.getWindows().在JavaFx中,有没有任何方法可以获得所有窗口的JavaFx应用程序?
谢谢,
对于运行java8的javafx8使用
FXRobotHelper.getStages()
or
StageHelper.getStages()
Run Code Online (Sandbox Code Playgroud)
这将检索所有Stages本质上是一个Window本身(它扩展了Window类)
AFAIK,仍然没有正确的方法来做到这一点.
虽然有一种肮脏的短期方式:
浏览源代码javafx.stage.Window,有一个静态的方法似乎做你期望的:javafx.stage.Window#impl_getWindows().
但是有一些免责声明:
/**
* Return all Windows
*
* @return Iterator of all Windows
* @treatAsPrivate implementation detail
* @deprecated This is an internal API that is not intended for use and will be removed in the next version
*/
@Deprecated
@NoInit
public static Iterator<Window> impl_getWindows() {
final Iterator iterator = AccessController.doPrivileged(
new PrivilegedAction<Iterator>() {
@Override public Iterator run() {
return windowQueue.iterator();
}
}
);
return iterator;
}
Run Code Online (Sandbox Code Playgroud)
这终于在 Java 9 中得到了正确修复。请参阅javafx.stage.Window.getWindows()
返回一个列表,其中包含对当前显示的 JavaFX 窗口的引用。该列表不可修改 - 尝试修改此列表将导致在运行时抛出 UnsupportedOperationException。
这在 Java 9 中是必不可少的,因为其他解决方案涉及StageHelper或FXRobotHelper不再可能,因为这些解决方案存在于com.sun.javafx无法再访问的包中。