无法初始化类javafx.stage.screen

时间:2015-08-29 02:12:18

标签: java javafx

我试图启动2个javaFX应用程序,显然每个JVM只能调用一次Application#launch()。 经过一番浏览之后,我告诉我手动创建一个场景并为第二个应用程序调用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把我的头骨弄得太长了。

1 个答案:

答案 0 :(得分:0)

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