运行JavaFX时应用程序启动方法异常

时间:2020-04-17 14:13:23

标签: java javafx

启动javaFX应用程序时出现java.lang.reflect.InvocationTargetException错误。有什么帮助吗?我相信这与.add(btns ...)有关,但是堆栈跟踪显示代码中没有任何一行引起此问题。

@Override
   public void start(Stage primaryStage) {
       GridPane root = new GridPane();

       for (int i = 0; i < game.getRows(); i++) {
           for (int j = 0; j < game.getColumns(); j++) {
               btns[i][j] = new Button(" ");
               root.add(btns[i][j], j, i, 1, 1);
           }
       }
       /*btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {

        }
        });*/
       Button newGame = new Button("New Game");
       Button quit = new Button("Quit");
       root.add(newGame, game.getColumns(), 0);
       root.add(quit, game.getColumns(), 1);
       Scene scene = new Scene(root);

       primaryStage.setTitle("Minesweeper");
       primaryStage.setScene(scene);
       primaryStage.show();
   }

这是完整的堆栈跟踪

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT
    at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
    at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
    at javafx.scene.layout.GridPane.add(GridPane.java:980)
    at part3.MinesweeperGUI.start(MinesweeperGUI.java:48)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
    ... 1 more
Exception running application part3.MinesweeperGUI
Java Result: 1

0 个答案:

没有答案
相关问题