【问题标题】:JavaFX Application Constructor in subclass [duplicate]子类中的JavaFX应用程序构造函数[重复]
【发布时间】:2018-05-17 09:19:26
【问题描述】:

尝试使用 javaFX 创建应用程序。此类扩展应用程序。 错误消息使我相信我的子类构造函数有问题,但它使用所有相关参数(无)调用 super(应用程序)。

public class SameGame extends Application {
private Button[][] board;
public Button[][] getBoard() {
    return board;
}
public SameGame(int BoardSize){
    super();
    board = new Button[BoardSize][BoardSize]; //only make squares
    for (int x = 0; x < getBoard().length; x++) {
        for (int y = 0; y < getBoard()[x].length; y++) {
            board[x][y] = new Button();
        }
    }
}
public void start(Stage primaryStage) {
    GridPane gridpane = new GridPane();
    Scene scene = new Scene(gridpane);
    primaryStage.setScene(scene);
    primaryStage.show();
    SameGame sameGame = new SameGame(5);

    for (int x = 0; x < getBoard().length; x++) {
        for (int y = 0; y < getBoard()[x].length; y++) {
            gridpane.add(getBoard()[x][y], x, y);
        }
    }
}

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

这是例外

Exception in Application constructor
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: Unable to construct Application instance: class SameGame
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchMethodException: SameGame.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getConstructor(Class.java:1825)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$160(LauncherImpl.java:818)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
    ... 1 more
Exception running application SameGame

Process finished with exit code 1

我知道答案就在那里,我只是不知道找到它

【问题讨论】:

  • 程序启动时如何选择正确的BoardSize值?
  • 嗯,应该可能来自命令行参数...
  • launch() 方法通过调用其无参数构造函数(通过反射)创建Application 类的实例。由于您没有无参数构造函数,因此您会收到错误消息。正如@DawoodibnKareem 指出的那样,您当前的代码无论如何都没有多大意义......
  • 也许它可能来自命令行,是的。但是现在,您的应用程序需要有一个构造函数public SameGame() 才能在启动时运行。例如,它可以像 public SameGame() { this(5); } 一样简单,它调用另一个构造函数。

标签: java inheritance javafx


【解决方案1】:

知道了! 同一游戏的构造函数不应该有任何参数,并且 BoardSize 的输入应该来自程序运行时的 cmd args

public SameGame(){
        int BoardSize = Integer.parseInt(this.getParameters().getRaw().get(0));

        board = new Button[BoardSize][BoardSize]; //get size of board fro cmd args
        for (int x = 0; x < getBoard().length; x++) {
            for (int y = 0; y < getBoard()[x].length; y++) {
                board[x][y] = new Button();
            }
        }

    }

【讨论】:

    猜你喜欢
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多