带有警报 showAndWait 的应用程序启动方法异常

Exception in application start method with alert showAndWait

我正在自学 JavaFX。我在论坛里搜索过,一直没有找到类似的问题。

我正在尝试获取关闭确认警报的按钮类型:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Group g = new Group();
        Scene scene = new Scene(g, 400, 400);
        primaryStage.setTitle("Hello World");
        primaryStage.show();
        primaryStage.setScene(scene);

        Alert a = new Alert(Alert.AlertType.CONFIRMATION);
        a.initOwner(primaryStage);
        a.setTitle("alert");
        a.setHeaderText("entête");
        a.setContentText("hi");
        a.show();

        a.showAndWait().ifPresent(response -> {
            if (response == ButtonType.OK) {
                System.out.println("rrrr");
            }
        });
    }

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

但是我有这个异常,我不知道为什么:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.IllegalStateException: Stage already visible
    at javafx.graphics/javafx.stage.Stage.showAndWait(Stage.java:451)
    at javafx.controls/javafx.scene.control.HeavyweightDialog.showAndWait(HeavyweightDialog.java:162)
    at javafx.controls/javafx.scene.control.Dialog.showAndWait(Dialog.java:346)
    at sample.Main.start(Main.java:29)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop(WinApplication.java:174)
    ... 1 more
Exception running application sample.Main

Process finished with exit code 1

我已经按照本教程的“JavaFX 和 IntelliJ”部分设置了我的 IDE:https://openjfx.io/openjfx-docs/

此问题是由于您尝试在已经可见的舞台上调用 showAndWait() 造成的。来自 javadocs

Throws: IllegalStateException - if this stage is already showing.

如果您删除初始调用 a.show(); 这应该可以解决您的问题。