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

查看:1707
本文介绍了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 另一个应用程序中的线程(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的update方法中运行这个新阶段。

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.

欢迎任何建议。谢谢。

推荐答案

应用程序不仅仅是一个窗口 - 它是一个进程。因此,每个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 class,只需将其包装在 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天全站免登陆