JavaFX 启动另一个应用程序 [英] JavaFX launch another application

查看:69
本文介绍了JavaFX 启动另一个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在用 JavaFx 砸我的脑袋...

I've been smashing my head with JavaFx...

这适用于没有运行应用程序实例的情况:

This works for when there's no instances of an application running:

public class Runner {

    public static void main(String[] args) {
        anotherApp app = new anotherApp();
        new Thread(app).start();
    }
 }

public class anotherApp extends Application implements Runnable {

    @Override
    public void start(Stage stage) {
    }

    @Override
    public void run(){
        launch();
    }
}

但是如果我在new Thread(app).start() 另一个应用程序,我会收到一个异常,说明我不能进行两次启动.

But if I do new Thread(app).start() within another application I get an exception stating that I can't do two launches.

我的方法也被另一个应用程序上的观察者调用,如下所示:

Also my method is called by an observer on the other application like this:

@Override
public void update(Observable o, Object arg) {
    // new anotherApp().start(new Stage());
            /* Not on FX application thread; exception */

    // new Thread(new anotherApp()).start();
            /* java.lang.IllegalStateException: Application launch must not be called more than once */
}

它在 JavaFX 类中,例如:

It's within a JavaFX class such as this:

public class Runner extends Applications implements Observer {

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

    @Override
    public void start(Stage stage){
    //...code...//
    }
    //...methods..//
    //...methods..//

    @Override
    public void update(Observable o, Object arg) {
    //the code posted above//
    }
}

我尝试将 ObjectProperties 与侦听器一起使用,但没有奏效.我需要以某种方式从 java.util.observer 的更新方法中运行这个新阶段.

I tried using ObjectProperties with listeners but it didn't work. I need to get this new stage running from within the update method from java.util.observer in some way.

欢迎提出任何建议.谢谢.

Any suggestions are welcomed. Thanks.

推荐答案

应用程序不仅仅是一个窗口——它还是一个 Process.因此,每个 VM 只允许一个 Application#launch().

Application is not just a window -- it's a Process. Thus only one Application#launch() is allowed per VM.

如果你想有一个新窗口 -- 创建一个 Stage.

If you want to have a new window -- create a Stage.

如果你真的想重用 anotherApp 类,只需将它包装在 Platform.runLater()

If you really want to reuse anotherApp class, just wrap it in Platform.runLater()

@Override
public void update(Observable o, Object arg) {
    Platform.runLater(new Runnable() {
       public void run() {             
           new anotherApp().start(new Stage());
       }
    });
}

这篇关于JavaFX 启动另一个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆