JavaFX 软件设计 [英] JavaFX software design

查看:24
本文介绍了JavaFX 软件设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaFX应用程序中,javafx.application.Application必须是子类,继承的launch()方法虽然是公共的,但必须从这个派生类中调用,否则会抛出异常.launch() 方法然后使用反射来实例化派生类,这使得在启动时很难为类成员设置值而不丢失它们.所有这些对我来说都显得很不寻常,我想知道为什么启动 JavaFX 应用程序如此复杂,如果这种软件设计(设计模式?)有名字,还是只是糟糕的设计?

In a JavaFX application, javafx.application.Application must be subclassed, and the inherited launch() method, although it's public, must be called from within this derived class, otherwise an exception is thrown. The launch() method then uses reflection to instantiate the derived class, making it difficult to set values for the class members without losing them when launching. All that appears totally unusual to me, and I was wondering why starting a JavaFX application is so complicated, if that kind of software design (design pattern?) has a name, or if it's just bad design?

更具体地说,我想使用观察者模式,因此我的 java 应用程序会在加载文档时收到通知,如下所示:

To be more specific, I want to use the observer pattern, so my java application gets notified when a document was loaded, like this:

public class MyDocumentLoader extends Application
{
    private ChangeListener<Worker.State> changeListener;

    public void setChangeListener(ChangeListener<Worker.State> changeListener)
    {
        this.changeListener = changeListener;
    }

    ...

    public void loadDocument(String url)
    {
        webEngine.getLoadWorker().stateProperty().addListener(changeListener);
        webEngine.load(url);
    }

    ...

}

我需要多个方法中的回调成员,理想情况下我可以有多个加载文档的类的实例,因此我可以为不同的 URL 设置不同的 ChangeListeners.

I need the callback member in several methods, and ideally I can have more than one instances of the class that loads documents, so I can set different ChangeListeners for different URLs.

推荐答案

JavaFX 支持大量部署和打包策略,参考.https://docs.oracle.com/javase/8/docs/technotes/guides/deploy/toc.html,并且拥有标准化的生命周期入口和出口点可以简化对所有这些策略的支持.

JavaFX supports a great number of deployment and packaging strategies, ref. https://docs.oracle.com/javase/8/docs/technotes/guides/deploy/toc.html, and having a standardized lifecycle entry- and exit-point simplifies supporting all these strategies.

如果您正在努力初始化您的主应用程序类,因为它被 JavaFX 启动器实例化,您最好的选择是使用 Application.init() 和 Application.stop() 方法,正如 James_D 指出的那样.

If you are struggling to initialize your main application class, due to it being instanciated by the JavaFX launcher, your best option is to use the Application.init() and Application.stop() methods, as James_D points out.

这篇关于JavaFX 软件设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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