JavaFX中的setDefaultCloseOperation()

cod*_*leb 3 javafx javafx-2

有没有办法在JavaFX中设置自定义close操作.我从swing中知道这个,并且找不到JavaFX.如果按[X]关闭窗口,它将自动关闭.

ass*_*ias 9

您可以使用该onCloseRequest属性:

stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override public void handle(WindowEvent t) {
        System.out.println("CLOSING");
    }
});
Run Code Online (Sandbox Code Playgroud)

请注意,如果您调用Platform.exit()代码,这将无效.