Close event in Java FX when close button is pressed

三世轮回 提交于 2020-12-06 09:27:08

问题


Is there any event handler present in Java FX, if i close a window directly bt pressing [X] button on Top right side. Which events gets fire in this case ? Nothing is working so far , neither setOnHiding not setOnCloseRequest()

Please help.


回答1:


Try this one

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        Text text = new Text("!");
        text.setFont(new Font(40));
        VBox box = new VBox();
        box.getChildren().add(text);
        final Scene scene = new Scene(box,300, 250);
        scene.setFill(null);
        stage.setScene(scene);
        stage.show();
        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
          public void handle(WindowEvent we) {
              System.out.println("Stage is closing");
          }
      });        
        stage.close();

    }

    public static void main(String[] args) {
        launch(args);
    }
}

Source Stage close event : Stage « JavaFX « Java



来源:https://stackoverflow.com/questions/38371117/close-event-in-java-fx-when-close-button-is-pressed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!