我希望我的 JavaFX 程序在我按下窗口右上角的 x 按钮时清理程序。我怎样才能做到这一点?清理代码是
JIntellitype.getInstance().cleanUp();
最佳答案
如果您有多个窗口并且清理只是针对分配给正在关闭的窗口的资源,请为窗口的 setOnHidden 添加适当的事件处理程序。事件。
Window.setOnHidden 事件的描述:
Called just after the Window has been hidden. When the Window is hidden, this event handler is invoked allowing the developer to clean up resources or perform other tasks when the Window is closed.
如果您为 JavaFX 应用程序创建多个阶段,应用程序将在应用程序的最后一个阶段关闭时关闭。当应用程序关闭时,应用程序的 stop方法被调用,您可以通过在 Application 类中覆盖它来实现。
Application.stop 方法的说明:
This method is called when the application should stop, and provides a convenient place to prepare for application exit and destroy resources.
在您的情况下,提供 Application.stop 的实现可能是最合适的解决方案。
关于java - 退出javafx程序时如何清理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9504901/