如何在不使用 .stopAndWait 方法的情况下在 Javafx 的警报中使用按钮?

How to use a button in an Alert in Javafx without using the .stopAndWait method?

我正在尝试在 javafx 中创建一个游戏,当您获胜时会弹出一条警告说您赢了,并且可以选择再次玩或关闭程序。 问题是在警报 window 中使用按钮,您必须使用不适用于时间轴的 alert.stopAndWait() 。

是否有另一种方法可以在没有这种方法的情况下控制按钮,或者是否有更好的编码方法?

在此先感谢您。

编辑: 到目前为止,这是我的警报代码:

 public static void alert(Alert.AlertType alertType, Window owner, String title, String message) {
    Alert alert = new Alert(alertType);
    alert.setHeaderText(null);
    alert.setTitle(title);
    alert.setContentText(message);
    alert.initOwner(owner);
    alert.show();
    ButtonType buttonPlayAgain = new ButtonType("Play again");
    alert.getButtonTypes().setAll(buttonPlayAgain);
    alert.setOnHidden(evt -> Platform.exit());

    Optional<ButtonType> result = alert.showAndWait();
    if (result.get() == buttonPlayAgain){
        // ... user chose Play Again"
        System.out.println("play again");
    } else
        Platform.exit();
        // if user clicks exit

问题是您不能将 showAndWait 与时间轴一起使用。我正在尝试寻找使用 showAndWait 的替代方法。

您可以使用 show() 方法,但您需要在 Alert 关闭后通过设置 onCloseRequest() 处理程序检索结果。

然后您可以使用 alert.getResult() 方法确定单击了哪个按钮。

这里有一个简单的程序来演示:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        // Simple interface
        VBox root = new VBox(5);
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);

        // Button to show an alert
        Button btnShowAlert = new Button("Show Alert!");

        // Setup the button action
        btnShowAlert.setOnAction(event -> {

            // Create a simple Alert
            Alert alert = new Alert(Alert.AlertType.NONE);
            alert.setHeaderText(null);
            alert.setTitle("Just a title");
            alert.setContentText("A fun message");
//            alert.initOwner(owner); // Remove for this sample

            ButtonType buttonPlayAgain = new ButtonType("Play again");
            alert.getButtonTypes().setAll(buttonPlayAgain);
//            alert.setOnHidden(evt -> Platform.exit()); // Don't need this

            // Listen for the Alert to close and get the result
            alert.setOnCloseRequest(e -> {
                // Get the result
                ButtonType result = alert.getResult();
                if (result != null && result == buttonPlayAgain) {
                    System.out.println("Play Again!");
                } else {
                    System.out.println("Quit!");
                }
            });

            alert.show();

        });

        root.getChildren().add(btnShowAlert);

        // Show the Stage
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}