JavaFX create alert and get result

后端 未结 2 535
耶瑟儿~
耶瑟儿~ 2021-01-22 17:29

For my CS class they require us to use JavaFX alerts. I can make an alert appear, but how do I get what button was clicked? What would be the best way to go about getting this d

2条回答
  •  天涯浪人
    2021-01-22 18:02

    You can do

    ButtonType queen = new ButtonType("Queen");
    ButtonType rook = new ButtonType("Rook");
    Alert a = new Alert(AlertType.NONE, "Promote pawn to:", queen, rook);
    a.setTitle("Title");
    a.setHeaderText("My header text");
    a.setResizable(true);
    a.setContentText("Content text");
    a.showAndWait().ifPresent(response -> {
        if (response == queen) {
            // promote to queen...
        } else if (response == rook) {
            // promote to rook...
        }
    });
    

提交回复
热议问题