java 如何关闭框架但打开新框架?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1921452/
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
how do i close a frame yet open a new frame?
提问by Tuffy G
how do i close a frame yet open a new frame?
如何关闭框架但打开新框架?
i have a frame, (help)
我有一个框架,(帮助)
when i click on my menu item i want to open (mainForm) exit from help.
当我点击我的菜单项时,我想打开 (mainForm) 从帮助中退出。
new mainForm().setVisible(true);
System.exit(0);
new mainForm().setVisible(true);
System.exit(0);
i know this closes the whole program however how do i get it to only close the current frame
我知道这会关闭整个程序但是我如何让它只关闭当前帧
thanks
谢谢
回答by Gordon
If you no longer want to use the frame you could use frame.dispose()
如果您不再想使用框架,您可以使用 frame.dispose()
If you just want to hide it use frame.setVisible(false).
如果您只想隐藏它,请使用frame.setVisible(false).
If you extended a Frame and are trying to close it from within use this.disposeor this.setVisible(false).
如果您扩展了一个 Frame 并试图从使用中关闭它,this.dispose或者this.setVisible(false).
回答by Michael Borgwardt
You should rethink your requirments. For the user, it would be best to have both the program and the help window visible at the same time. Closing the main window when showing the help screen and vice versa is really, really badfor usability - you shouldn't do it. We've had window-based GUIs for almost 30 years now - showing several windows on screen at the same time is what they're for!
你应该重新考虑你的要求。对于用户来说,最好让程序和帮助窗口同时可见。在显示帮助屏幕时关闭主窗口,反之亦然,这对可用性非常非常不利- 您不应该这样做。我们已经拥有基于窗口的 GUI 近 30 年了——它们的用途是在屏幕上同时显示多个窗口!
回答by TStamper
Let's say you created your frame as so:
假设您是这样创建框架的:
JFrame mainframe = new JFrame("Radio Notes");
//show Frame
mainframe.setVisible(true);
//close the frame
mainframe.dispose();
回答by extraneon
I think you should hide the frame you do not wish shown with setVisible(false). System.exit(0) stops the JVM ending the entire program.
我认为你应该隐藏你不希望用 setVisible(false) 显示的框架。System.exit(0) 停止 JVM 结束整个程序。
In short, the semantics of setVisible has nothing to do with starting / stopping the application.
简而言之,setVisible 的语义与启动/停止应用程序无关。
If you want to start an entire new application you'd have to look at Runtime.exec(). I don't really know if you can close the parent process (the Help application) with exit() though.
如果您想启动一个全新的应用程序,您必须查看 Runtime.exec()。我真的不知道您是否可以使用 exit() 关闭父进程(帮助应用程序)。
回答by ChadNC
try setting the default close operation for the JFrame like so.
尝试像这样设置 JFrame 的默认关闭操作。
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Then implement a WindowListenerthat performs the actions you want when a window closing event is fired.
然后实现一个WindowListener,它在触发窗口关闭事件时执行您想要的操作。

