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

Java Swing:如何检查在运行时打开了哪些组件?

  •  0
  • skonline90  · 技术社区  · 8 年前

    我正在使用Java Swing,有以下问题:

    是否有办法查看用户在运行时打开的顶级容器的类型?

    我的UI上有一个按钮,可以打开 JFrame 由于不能使其成为模态,用户可以打开无数帧。我想阻止用户这样做。(以防万一:我不想切换到 JDialog :))

    2 回复  |  直到 8 年前
        1
  •  2
  •   Thomas Fritsch    8 年前

    您可以使用静态方法 Window.getWindows()

    返回全部的数组 Window s、 无论是自有还是无主, 由此应用程序创建。

    因此,您将获得所有框架和对话框。

    您可能需要通过 isShowing() 跳过当前关闭的窗口。

        2
  •  0
  •   skonline90    8 年前

    多亏了托马斯!

                    Window[] openedWindows = Window.getWindows();
                    int countOfShowingWindows = 0;
                    for (Window window : openedWindows)
                    {
                        if (window.isShowing()) ++countOfShowingWindows;
                    }
                    if (countOfShowingWindows == 1)
                    {
                        // Open the Dialog
                    }