我一直在用 JavaFx 砸脑袋......
这适用于没有运行应用程序实例的情况:
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()
,我会收到一个异常,指出我无法执行两次启动。
我的方法也被其他应用程序的观察者调用,如下所示:
@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 类中,如下所示:
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 的更新方法中运行这个新阶段。
欢迎提出任何建议。谢谢。
最佳答案
应用程序不仅仅是一个窗口——它是一个进程
。因此每个 VM 只允许一个 Application#launch()
。
如果你想要一个新窗口——创建一个Stage
。
如果你真的想重用anotherApp
类,只需将它包装在Platform.runLater()
@Override
public void update(Observable o, Object arg) {
Platform.runLater(new Runnable() {
public void run() {
new anotherApp().start(new Stage());
}
});
}
关于JavaFX 启动另一个应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15653118/