Java:如何在打开另一个 JFrame 时关闭一个 JFrame?
我的程序以 JFrame 中带有文本字段的图片开始。我希望当用户输入 start 时,它会关闭图片 JFrame 并使用主程序打开另一个 JFrame。我已经在图像框架上尝试过
processEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); ,
但它关闭了所有窗口。
My program starts with a picture with a textfield in a JFrame. I want when the user types start it closes the picture JFrame and opens another JFrame with the main program. I've tried
processEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
on the Image frame but it closes all the windows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
方法
JFrame.setVisible
可用于根据参数隐藏或显示JFrame,而JFrame.dispose
实际上会通过关闭并释放框架来“销毁”框架增加它使用的资源。在这里,如果您打算重新打开相框,则可以在相框上调用setVisible(false)
;如果您不打算打开相框,则可以在相框上调用dispose()
再次,这样你的程序就可以释放一些内存。然后,您可以在主框架上调用setVisible(true)
使其可见。The method
JFrame.setVisible
can be used to hide or display the JFrame based on the arguments, whileJFrame.dispose
will actually "destroy" the frame, by closing it and freeing up resources that it used. Here, you would callsetVisible(false)
on the picture frame if you intend to reopen it, or calldispose()
on the picture frame if you will not be opening it again, so your program can free some memory. Then you would callsetVisible(true)
on the main frame to make it visible.您也可以使用此代码
例如,
you also can use this code
for example
也许如果你设置图片JFrame的 默认关闭操作除了
EXIT_ON_CLOSE
之外,也许是DISPOSE_ON_CLOSE
,您可以防止应用程序在第二个JFrame出现之前关闭。Maybe if you set the picture JFrame's default close operation to something besides
EXIT_ON_CLOSE
, perhapsDISPOSE_ON_CLOSE
, you can prevent your application from closing before the second JFrame appears.我正在寻找同样的东西,发现使用“this”是最好和最简单的选择。
您可以使用以下代码:
this.dispose();
I was searching for the same thing and found that using "this" is the best and easiest option.
you can Use the following code:
this.dispose();
这篇文章有点旧,但仍然如此。
如果您像这样初始化表单:
例如通过按钮创建或打开另一个表单:
这将在不退出程序的情况下处理并销毁第一个窗口。
关键是设置
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
。它还引发事件(我已经使用 WindowClosing 事件对其进行了测试)。
This post is a bit old but nevertheless.
If you initialize the form like that:
And for instance create or open another form by a button:
This will dispose and destroy the first window without exiting the program.
The key is to set
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
.It also raises the events (I've tested it with the
WindowClosing
event).这是我对这个问题的解决方案:
Here is my solution to this problem:
对于 netbeans,使用当前对象的引用和
setVisible(false);
例如
For netbeans use the reference of the current Object and
setVisible(false);
for example
你也可以使用这个:
you also can use this :
首先调用它
First call it
这就是我在关闭另一个 jframe 时打开一个新 jframe 的想法:
This is what i came up for opening a new jframe while closing the other one: