2016-04-25 23 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); 
     } 
    }); 
}