如何通过单击按钮关闭JFrame?

编程入门 行业动态 更新时间:2024-10-28 20:26:50
本文介绍了如何通过单击按钮关闭JFrame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在窗口中有一个按钮,以便如果单击它(按钮),则窗口将关闭.

I would like to have a button in my window such that if I click it (button) the window is closed.

我发现我可以通过以下方式关闭窗口:

I found out that I can close a window in the following way:

referenceToTheFrame.hide(); //hides the frame from view refToTheFrame.dispose(); //disposes the frame from memory

但是,如果我这样做,编译器会抱怨:

But if I do this way, compiler complains:

Note: myProgram.java uses or overrides a deprecated API Note: Recompile with -Xlint:deprication for details.

我做不安全的事吗?

推荐答案

推荐的使用方法是:

referenceToTheFrame.setVisible(false)

hide方法已被弃用,不应再使用. (尽管在内部setVisible会调用hide或show)

The hide method is deprecated and should not longer be used. (Although internally setVisible will call hide or show)

此外,如果要放置框架,则必须手动调用dispose. (例如,如果您需要关闭窗口的事件)对setDefaultCloseOperation的调用对您无济于事,因为它只会影响在系统菜单上单击关闭按钮后的行为.

Additionally if you want to dispose the frame, you have to call dispose manually. (In case you need the window-closed event for example) A call to setDefaultCloseOperation doesn't help you here, because it only affects the behavior after a click on the close button from the system menu.

请参阅 Swing文档进一步的信息.

更多推荐

如何通过单击按钮关闭JFrame?

本文发布于:2023-11-08 11:56:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569304.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   按钮   JFrame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!