java - 退出javafx程序时如何清理程序?

标签 java user-interface javafx-2 exit-code

我希望我的 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/

相关文章:

objective-c - OS X 10.8+ 中带有 Finder 等部分的 NSBrowser

user-interface - 如何在 Amazon AWS 中将 ubuntu 服务器升级到桌面?

java - 如何从Choicebox获取int值

java - 如何让 JavaFX 2.0 ChoiceBox 选择它的第一个元素

java - 如何在Android中压缩RAR格式的文件?

Java 运算符重载

JavaFX 2.2 : buttons goes out of the Vbox layout when shrinking the stage

java - 未能找到 getChildren(),遵循 JavaFX WebKit 的 Oracle 官方指南

java - 使用 SecurityFilter 在 Tomcat 上进行 JAAS 身份验证

java - 从 H2 数据库的大表中选择随机行