无法初始化javafx.stage.screen类



我正在尝试启动2个javaFX应用程序,显然application# launch()只能在每个JVM中调用一次。浏览了一会儿之后,有人告诉我手动创建一个场景,并为第二个应用程序调用application# start(),所以我这样做了:

public class Launcher extends Application {
private Stage primaryStage;
@Override
public void start(Stage primaryStage) throws Exception {
    this.primaryStage = primaryStage;
    this.primaryStage.setResizable(false);
    this.primaryStage.setTitle("title");
    initLayout();
}
public void initLayout() throws IOException {
    Parent root = FXMLLoader.load(Launcher.class.getResource("myFile.fxml"));
    Scene scene = new Scene(root, 450, 300);
    this.primaryStage.setScene(scene);
    this.primaryStage.show();
}
}

并加载它(从另一个类):

    try {
        Application launcher = new Launcher();
        launcher.start(new Stage());
    } catch (Exception e) {}

虽然这会导致错误说

Exception in thread "Thread_number" java.lang.NoClassDefFoundError: Could not initialize class javafx.stage.Screen 
at javafx.stage.Window.<init><Unknown Source>
at javafx.stage.Stage.<init><Unknown Source>
at javafx.stage.Stage.<init><Unknown Source>
at javafx.stage.Stage.<init><Unknown Source>
at classILaunchedFrom.methodLaunchedFrom<Main.java:lineNumber>
有人知道我做错了什么吗,因为我完全不知道。使用javaFX已经太久了

为什么要完全分离应用程序?只需为第二个窗口创建一个扩展stage的类,然后从主javafx应用程序线程内部调用它的.show()方法。Javafx在做事时相当挑剔,一次只处理一个应用程序实例,所有工作必须在同一个线程上完成,等等。

最新更新