我有以下3行代码:
1 System.out.println("Starting program...");
2 Application.launch((Gui.class));
3 System.out.println("Continuing program...");问题是,当javafx应用程序启动后,第2行代码直到我关闭javafx应用程序才会执行。在javafx应用程序仍在运行时,启动javafx应用程序并执行第3行的正确方法是什么?
编辑1
到目前为止我找到的唯一解决办法是:
2 (new Thread(){
public void run(){
Application.launch((Gui.class));
}
}).start();对于javafx应用程序来说,这种解决方案正常且安全吗?
发布于 2015-05-25 17:37:03
我不知道您要做什么,但是Application.launch也在等待应用程序完成,这就是为什么您没有立即看到第3行的输出。您的应用程序的start方法是您想要进行设置的地方。有关更多信息和示例,请参见应用程序类的API文档。
编辑:如果您想从一个主线程运行多个JavaFX应用程序,也许这就是您所需要的:
public class AppOne extends Application
{
@Override
public void start(Stage stage)
{
Scene scene = new Scene(new Group(new Label("Hello from AppOne")), 600, 400);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args)
{
System.out.println("Starting first app");
Platform.runLater(() -> {
new AppOne().start(new Stage());
});
System.out.println("Starting second app");
Platform.runLater(() -> {
new AppTwo().start(new Stage());
});
}
}
public class AppTwo extends Application
{
@Override
public void start(Stage stage)
{
Scene scene = new Scene(new Group(new Label("Hello from AppTwo")), 600, 400);
stage.setScene(scene);
stage.show();
}
}这通过在JavaFX线程上运行它们的start方法从主线程运行多个应用程序。但是,您将失去init和stop生命周期方法,因为您没有使用Application.launch。
https://stackoverflow.com/questions/30443322
复制相似问题