在 Java 应用程序中在 Windows 之间切换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5124547/
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
Switching Between Windows in Java Applcation
提问by mike9182
I am creating a Java Application using netBeans and Swing Gui Builder. I am trying to create an application that closes the current window and opens a new one when a selection is made from the view menu. What would be the best way to do this?
我正在使用 netBeans 和 Swing Gui Builder 创建一个 Java 应用程序。我正在尝试创建一个应用程序,该应用程序在从视图菜单中进行选择时关闭当前窗口并打开一个新窗口。什么是最好的方法来做到这一点?
EDIT: I am trying to create a desktop application.
编辑:我正在尝试创建一个桌面应用程序。
回答by donnyton
If you only have 2 windows that you want to swap between, it may be easiest to just use JFrame.setVisible() to swap between the two.
如果您只想在 2 个窗口之间进行交换,那么使用 JFrame.setVisible() 在两者之间进行交换可能是最简单的。
frame1.setVisible(false); //hides it temporarily
frame2.setVisible(true); //shows it
This doesn't actually close frame1--it just hides it and pops frame 2 into visibility.
这实际上并没有关闭第 1 帧——它只是隐藏它并将第 2 帧弹出到可见状态。
If you are writing a program with many potential windows and you want to actually "destroy" the window (thus freeing up the extra memory it takes up) you need to call JFrame.dispose();
如果您正在编写具有许多潜在窗口的程序,并且您想真正“销毁”该窗口(从而释放它占用的额外内存),则需要调用 JFrame.dispose();
frame1.dispose(); //closes the window--cannot be recovered
frame2.setVisible(true); //shows it