1
這是我的開始 - 方法。首先我創建一個舞臺並設置標題和場景。如果有人想關閉window-close-btn [X]上的窗口,我想創建一個對話框。我想我會用setOnCloseRequest()函數來捕獲這個事件。但我仍然可以關閉運行時打開的所有階段。沒有函數的JavaFX stage.setOnCloseRequest?
@Override
public void start(final Stage primaryStage) throws Exception {
primaryStage.setTitle("NetControl");
primaryStage.setScene(
createScene(loadMainPane())
);
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(final WindowEvent event) {
//Stage init
final Stage dialog = new Stage();
dialog.initModality(Modality.APPLICATION_MODAL);
// Frage - Label
Label label = new Label("Do you really want to quit?");
// Antwort-Button JA
Button okBtn = new Button("Yes");
okBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
dialog.close();
}
});
// Antwort-Button NEIN
Button cancelBtn = new Button("No");
cancelBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
primaryStage.show();
dialog.close();
}
});
}
});
primaryStage.show();
}
private Pane loadMainPane() throws IOException {
FXMLLoader loader = new FXMLLoader();
Pane mainPane = (Pane) loader.load(
getClass().getResourceAsStream(ContentManager.DEFAULT_SCREEN_FXML)
);
MainController mainController = loader.getController();
ContentManager.setCurrentController(mainController);
ContentManager.loadContent(ContentManager.START_SCREEN_FXML);
return mainPane;
}
private Scene createScene(Pane mainPane) {
Scene scene = new Scene(mainPane);
setUserAgentStylesheet(STYLESHEET_MODENA);
return scene;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Application.launch(args);
}
是否有任何其他函數來捕捉窗口事件? 或不合邏輯地在primaryStage上運行CloseRequest,我讀了一些平臺(但我不知道是否有必要爲我的問題)?
感謝您的快速回答! 它解決了我的問題;) – malex