为 setDefaultCloseOperation 创建自定义操作?
我想让我的 Java 应用程序在按下“关闭”十字按钮时调用我自己的自定义函数。据我所知,可能没有办法,因为 setDefaultCloseOperation 根本没有重载。
知道如何实现这一点吗?
I want to make my Java Application to call my own custom made function when the "close" cross button is pressed. as far as i see there may be no way since setDefaultCloseOperation
has no overloads at all.
Any idea how this can be achieved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许是这个,但在此之前请阅读 Howard 发布的教程 WindowListener ,有一些/其他选项
maybe this one, but before that read tutorial WindowListener posted by Howard, there are some/another options
上述所有建议都是正确的,因为您需要使用 WindowListener。
然而,所有答案也不完整,因为他们忘记提及您可能还想添加:
这将允许您的代码完全控制窗口关闭过程,因为窗口不会自动关闭,除非您告诉它(通常通过在框架上使用 dispose() 方法)。这允许您提示用户确认是否关闭窗口。
关闭应用程序有一个简单的 API,允许您创建关闭窗口时执行的简单操作。它为您管理关闭操作和窗口侦听器代码。
All the above suggestions are correct in that you need to use a WindowListener.
However all the answer are also incomplete in that they forget to mention that you may also want to add:
This will allow your code to take full control of the window closing process as the window will not automatically close unless you tell it to (generally by using the dispose() method on the frame). This allow you to promt the user for a confirmation to close the window or not.
Closing an Application has a simple API that allows you to create a simple Action that is executed when the window is closed. It manages the close operation and the window listener code for you.
您可以为框架添加窗口侦听器:
You can add a window listener for the frame:
您可以将
WindowListener
添加到框架中(请参阅例如 这里)。You can add a
WindowListener
to your frame (see e.g. here).