JavaFX 启动内部应用程序 class

JavaFX launch inner Application class

我正在尝试从封闭的 class 启动内部应用程序 class,但它不起作用。

我已经尝试从 Window 中删除静态修饰符并从 Main 的实例启动它。

代码:

import javafx.application.Application;
import javafx.stage.Stage;

public class Main {
    private static class Window extends Application {
        @Override
        public void start(Stage stage) {
            stage.show();
        }
    }

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

异常:

Exception in Application constructor
Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class VirtualLaunchpad$Window
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchMethodException: VirtualLaunchpad$Window.<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(LauncherImpl.java:818)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null(GtkApplication.java:139)
    ... 1 more

Application subclass(及其无参数构造函数)必须是 public。 Application.launch() 方法在通过反射实例化应用程序 class 时仅搜索 public 无参数构造函数。

import javafx.application.Application;
import javafx.stage.Stage;

public class Main {
    public static class Window extends Application {
        @Override
        public void start(Stage stage) {
            stage.show();
        }
    }

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