在 Java 应用程序中获取任何/所有活动的 JFrame?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7364535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:41:13  来源:igfitidea点击:

Get Any/All Active JFrames in Java Application?

javaswinglistmethodsjframe

提问by Connor Neville

Is there a way from within a Java application to list all of the currently open/active (I'm not sure the terminology here) JFramesthat are visible on screen? Thanks for your help.

有没有办法从 Java 应用程序中列出JFrames屏幕上可见的所有当前打开/活动(我不确定这里的术语)?谢谢你的帮助。

回答by Andrew Thompson

Frame.getFrames()returns an array of all frames.

Frame.getFrames()返回所有帧的数组。

Alternately as mentioned by @mKorbel, Window.getWindows()will return all windows - since Frame(& JFrame) extend Windowthat will provide all frames, and then some. It will be necessary to iterate them to discover which ones are currently visible.

或者,如@mKorbel 所述,Window.getWindows()将返回所有窗口 - 因为Frame(& JFrame) 扩展Window将提供所有框架,然后是一些。有必要迭代它们以发现哪些当前可见。

回答by mike rodent

I agree with Stefan Reich's comment.

我同意 Stefan Reich 的评论。

A very useful method is Window.getOwnedWindows()... and one context where it is useful, if not essential, is TDD (Test-Driven Development): in an (integration) test, where you have various Windowobjects on display (JDialog, etc.), if something goes wrong before the test has finished normally (or even if it finishes normally), you will often want to dispose of subordinate windows in the tests' clean-up code. Something like this (in JUnit):

一个非常有用的方法是Window.getOwnedWindows()......并且一个有用的上下文是 TDD(测试驱动开发):在(集成)测试中,您可以Window在其中显示各种对象(JDialog等),如果在测试正常完成之前出现问题(或者即使它正常完成),您通常希望在测试的清理代码中处理从属窗口。像这样(在 JUnit 中):

@After
public void executedAfterEach() throws Exception {
    // dispose of dependent Windows...
    EventQueue.invokeAndWait( new Runnable(){
        @Override
        public void run() {
            if( app.mainFrame != null ){
                for( Window window : app.mainFrame.getOwnedWindows() ){
                    if( window.isVisible() ){
                        System.out.println( String.format( "# disposing of %s", window.getClass() ));
                        window.dispose();
                    }
                }
            }
        }
    });
}

回答by Blasanka

Frame.getFrames()will do your work.

Frame.getFrames()会做你的工作。

From oracle Doc:

来自 oracle 文档:

Returns an array of all Frames created by this application. If called from an applet, the array includes only the Frames accessible by that applet.

返回此应用程序创建的所有帧的数组。如果从小程序调用,则该数组仅包含该小程序可访问的帧。

A simple example:

一个简单的例子:

//all frames to a array
Frame[] allFrames = Frame.getFrames();

//Iterate through the allFrames array
for(Frame fr : allFrames){
    //uncomment the below line to see frames names and properties/atr.
    //System.out.println(fr);        

    //to get specific frame name
    String specificFrameName = fr.getClass().getName();

    //if found frame that I want I can close or any you want
    //GUIS.CheckForCustomer is my specific frame name that I want to close.
    if(specificFrameName.equals("GUIS.CheckForCustomer")){
        //close the frame
        fr.dispose();
    }
}

Also you can use Window.getWindows()as others mentioned.

你也可以Window.getWindows()像其他人提到的那样使用。