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

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

我正在自学javafx。我在论坛上搜索过,但一直找不到类似的问题。
我正在尝试获取用于关闭确认警报的按钮类型:

  1. package sample;
  2. import javafx.application.Application;
  3. import javafx.fxml.FXMLLoader;
  4. import javafx.scene.Group;
  5. import javafx.scene.Parent;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.Alert;
  8. import javafx.scene.control.ButtonType;
  9. import javafx.stage.Stage;
  10. public class Main extends Application {
  11. @Override
  12. public void start(Stage primaryStage) throws Exception{
  13. Group g = new Group();
  14. Scene scene = new Scene(g, 400, 400);
  15. primaryStage.setTitle("Hello World");
  16. primaryStage.show();
  17. primaryStage.setScene(scene);
  18. Alert a = new Alert(Alert.AlertType.CONFIRMATION);
  19. a.initOwner(primaryStage);
  20. a.setTitle("alert");
  21. a.setHeaderText("entête");
  22. a.setContentText("hi");
  23. a.show();
  24. a.showAndWait().ifPresent(response -> {
  25. if (response == ButtonType.OK) {
  26. System.out.println("rrrr");
  27. }
  28. });
  29. }
  30. public static void main(String[] args) {
  31. launch(args);
  32. }
  33. }

但我有个例外,我不知道为什么:

  1. Exception in Application start method
  2. java.lang.reflect.InvocationTargetException
  3. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  4. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
  5. at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  6. at java.base/java.lang.reflect.Method.invoke(Method.java:564)
  7. at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
  8. at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
  9. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  10. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
  11. at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  12. at java.base/java.lang.reflect.Method.invoke(Method.java:564)
  13. at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
  14. Caused by: java.lang.RuntimeException: Exception in Application start method
  15. at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
  16. at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
  17. at java.base/java.lang.Thread.run(Thread.java:832)
  18. Caused by: java.lang.IllegalStateException: Stage already visible
  19. at javafx.graphics/javafx.stage.Stage.showAndWait(Stage.java:451)
  20. at javafx.controls/javafx.scene.control.HeavyweightDialog.showAndWait(HeavyweightDialog.java:162)
  21. at javafx.controls/javafx.scene.control.Dialog.showAndWait(Dialog.java:346)
  22. at sample.Main.start(Main.java:29)
  23. at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
  24. at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
  25. at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
  26. at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
  27. at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
  28. at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
  29. at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  30. at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
  31. ... 1 more
  32. Exception running application sample.Main
  33. Process finished with exit code 1

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

c2e8gylq

c2e8gylq1#

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

相关问题