JavaFX软件设计 [英] JavaFX software design

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

问题描述

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



编辑:



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

  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);
}

...

}


$ b $我需要几个方法的回调成员,理想情况下,我可以有多个加载文档的类的实例,所以我可以为不同的URL设置不同的ChangeListener。

解决方案

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



如果您正在努力初始化您的主要应用程序类,由于它由JavaFX启动器实现,您最好的选择是使用Application.init()和Application.stop()方法,如James_D指出的。


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?

EDIT:

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);
    }

    ...

}

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 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.

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天全站免登陆