2016-04-25 53 views

回答

1

當存在退出事件(來自工具欄或菜單操作)或關閉請求事件時,當前實現使用Alert對話框來顯示消息。

雖然此對話框不是可自定義的,但有一個showCloseConfirmation屬性可讓您取消該對話框,因此您可以靜默地退出該應用程序,也可以提供自己的對話框。

例如,基於與膠子插件創建的默認單一的桌面項目,我們可以修改exit行動MenuActions下:

@Inject 
ParticleApplication app; 

@ActionProxy(text="Exit", accelerator="alt+F4") 
private void exit() { 
    // disable built-in dialog 
    app.setShowCloseConfirmation(false); 
    // create a custom dialog 
    Alert dialog = new Alert(Alert.AlertType.CONFIRMATION, "Custom exit Message"); 
    Optional<ButtonType> result = dialog.showAndWait(); 
    if(result.isPresent() && result.get().equals(ButtonType.OK)) { 
     app.exit(); 
    } 
} 

此外,您將需要處理關閉請求事件,在主類,消耗這些事件,並呼籲你的行動:

@Override 
public void postInit(Scene scene) { 
    ... 
    scene.windowProperty().addListener(new InvalidationListener() { 
     @Override 
     public void invalidated(Observable observable) { 
      scene.getWindow().setOnCloseRequest(e -> { 
       e.consume(); 
       action("exit").handle(new ActionEvent()); 
      }); 

      scene.windowProperty().removeListener(this); 
     } 
    }); 
}