gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 04:07:04 24 4
gpt4 key购买 nike

我想在我的窗口中有一个按钮,这样如果我单击它(按钮),窗口就会关闭。

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

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 会调用 hideshow)

此外,如果你想处置框架,你必须手动调用dispose。 (例如,如果您需要窗口关闭事件)调用 setDefaultCloseOperation 在这里对您没有帮助,因为它只会影响点击系统菜单中的关闭按钮后的行为。

参见 Swing documentation了解更多信息。

关于java - 如何通过单击按钮关闭 JFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2599993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com