简体   繁体   English

禁用移动JavaFX警报

[英]Disable movement of a javafx alert

I have a problem with my "Do you want to quit" interface. 我的“您想退出”界面有问题。

As of today I can move the alert box where I want, which I would like to change - preferably fixating the alert in a set position (in the middle of the screen). 从今天起,我可以将警报框移动到想要更改的位置,最好将警报固定在设定的位置(在屏幕中间)。

The problem with this alert being able to move around is that on setups with more than one screen you could move it wherever you would like (out of the game interface fe). 该警报能够四处移动的问题在于,在具有多个屏幕的设置中,您可以将其移动到任意位置(超出游戏界面fe)。

    Button btExit = new Button("Exit Game");
    btExit.setMinWidth(100);
    buttonGrid.add(btExit, 0, 5);
    btExit.setOnAction(new EventHandler<ActionEvent>() 
    {
        @Override
        public void handle(ActionEvent event) 
        {
            Alert alert = new Alert(AlertType.CONFIRMATION);
            alert.setTitle("");
            alert.initModality(Modality.APPLICATION_MODAL);
            alert.initOwner(primaryStage);
            alert.setHeaderText("Quit Game");
            alert.setContentText("Are you sure?");

            Optional<ButtonType> result = alert.showAndWait();
            if (result.get() == ButtonType.OK){
                System.exit(1);
            }else 
            {
                alert.close();
            }

        }
    });

If you want to prevent the user from moving the dialog, simply set the style to UNDECORATED which removes the window border the user could use to move around the alert (as well as the x-button, but the user has a alternative way of closing the dialog): 如果要阻止用户移动对话框,只需将样式设置为UNDECORATED删除用户可以用来在警报周围移动的窗口边框(以及x按钮),但是用户可以使用另一种关闭方式对话框):

@Override
public void start(Stage primaryStage) {
    Button btn = new Button("Show alert");
    btn.setOnAction((ActionEvent event) -> {
        Alert alert = new Alert(AlertType.CONFIRMATION);
        alert.getDialogPane().setStyle("-fx-border-color: black;");
        alert.initModality(Modality.APPLICATION_MODAL);

        alert.initStyle(StageStyle.UNDECORATED);

        alert.initOwner(primaryStage);
        alert.setHeaderText("Quit Game");
        alert.setContentText("Are you sure?");

        Optional<ButtonType> result = alert.showAndWait();
        if (result.orElse(null) == ButtonType.OK) {
            Platform.exit();
        }
    });

    StackPane root = new StackPane();
    root.getChildren().add(btn);

    Scene scene = new Scene(root, 600, 400);

    primaryStage.setScene(scene);
    primaryStage.show();
}



Some other modifications I did here: 我在这里做了一些其他修改:

  • Replace 更换

     System.exit(1); 

    with

     Platform.exit(); 

    To shut down the application more gracefully. 要更正常地关闭应用程序。 After all a status code of 1 indicates abnormal termination . 毕竟,状态码1表示异常终止

  • Remove 去掉

     else { alert.close(); } 

    this is never necessary, since showAndWait only returns after the Alert is closed anyways... 这从来没有必要,因为showAndWait仅在Alert关闭后才返回...

  • Used 用过的

     result.orElse(null) 

    to retrieve the value without an error, even if the Optional is empty (not 100% sure this can happen in this case, but it does not hurt to do it like this). 即使Optional为空也可以毫无错误地检索该值(不是100%确保在这种情况下会发生这种情况,但是这样做不会有任何伤害)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM