加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 05:16:14 所属栏目:Java 来源:网络整理
导读:我想在我的窗口中有一个按钮,如果我点击它(按钮),窗口就会关闭. 我发现我可以通过以下方式关闭窗口: referenceToTheFrame.hide(); //hides the frame from viewrefToTheFrame.dispose(); //disposes the frame from memory 但如果我这样做,编译器抱怨: Not
我想在我的窗口中有一个按钮,如果我点击它(按钮),窗口就会关闭.

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

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

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

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

我做一些不安全的事吗?

解决方法

推荐的方法是使用:

referenceToTheFrame.setVisible(false)

不推荐使用hide方法,不应再使用. (虽然内部setVisible会调用hide或show)

此外,如果要丢弃框架,则必须手动调用dispose. (例如,如果您需要窗口关闭事件)调用setDefaultCloSEOperation对此没有帮助,因为它只影响从系统菜单中单击关闭按钮后的行为.

有关详细信息,请参阅Swing documentation.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读