应用程序启动方法中出现异常,并发出警报showandwait

myss37ts  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(451)

我正在自学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$2(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$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(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$3(WinApplication.java:174)
    ... 1 more
Exception running application sample.Main

Process finished with exit code 1

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

c2e8gylq

c2e8gylq1#

这个问题是由于您试图呼叫 showAndWait() 在一个已经看得见的舞台上。来自javadocs
抛出: IllegalStateException -如果这个阶段已经开始了。
如果你删除了最初的通话 a.show(); 这会解决你的问题。

相关问题