如何在 Eclipse 中将 javaFX Preloader 与独立应用程序一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15126210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:04:51  来源:igfitidea点击:

How to use javaFX Preloader with stand-alone application in Eclipse?

eclipseosgijavafxpreloader

提问by Keon Wang

My IDE is eclipse and my project is a stand-alone javaFX application (pure CS architecture with OSGI framework).

我的 IDE 是 eclipse,我的项目是一个独立的 javaFX 应用程序(带有 OSGI 框架的纯 CS 架构)。

How to use Preloader thus the preloader would be started before my main Application and hid later?

如何使用预加载器,因此预加载器将在我的主应用程序之前启动并稍后隐藏?

I found some code in http://docs.oracle.com/javafx/2/deployment/preloaders.htm#BABGGDJG

我在http://docs.oracle.com/javafx/2/deployment/preloaders.htm#BABGGDJG 中找到了一些代码

But I still don't know how to deploy the Preloader with my startup Application in a OSGI framework. I give some code of my startup application below:

但是我仍然不知道如何在 OSGI 框架中使用我的启动应用程序部署预加载器。我在下面给出了我的启动应用程序的一些代码:

public class MyPrjMain extends Application {
    private static Stage primaryStage;

    public void start(final Stage stage) throws BusinessException {
        primaryStage = stage;

        init(primaryStage);

        primaryStage.show();
    }
}

Thanks very much, everybody.

非常感谢大家。

回答by jewelsea

This is a long answer, the quick answer for the impatient is to download this sample code for displaying a splash page for an intensive startup taskand see if it is adaptable to your situation.

这是一个很长的答案,不耐烦的快速答案是下载此示例代码以显示密集启动任务的启动页面,看看它是否适合您的情况。



My answer provides general information about Preloaderstyle functionality in JavaFX. Your question specifically mentions Preloaderusage in an Eclipse and OSGI environment, but I won't directly address that scenario as I don't use those technologies. Hopefully the general information is still applicable to your scenario.

我的回答提供了有关JavaFX 中预加载器样式功能的一般信息。您的问题特别提到了Preloader在 Eclipse 和 OSGI 环境中的使用,但我不会直接解决这种情况,因为我不使用这些技术。希望一般信息仍然适用于您的场景。

1. Java has native support for displaying a splash page when Java is started.

1. Java 原生支持在 Java 启动时显示启动页面。

  • This works using the -splash:<image>VM switch.

Advantages and disadvantages:

的优点和缺点:

+The simplest way to get your standalone application to show a splash image.

+让独立应用程序显示启动图像的最简单方法。

+Can be displayed very quickly => it's an argument input to the VM process, so (presumably) it can be displayed even before the VM itself has fully initialized.

+可以非常快速地显示 => 它是 VM 进程的参数输入,因此(大概)它甚至可以在 VM 本身完全初始化之前显示。

-Has limited features => only allows display of an image, not other preloader features such as reporting of initialization progress, animation, login prompts etc (unless you make use of AWT APIs)

-功能有限 => 只允许显示图像,不能显示其他预加载器功能,例如报告初始化进度、动画、登录提示等(除非您使用 AWT API

-Won't work on all platforms until Java 8 (see issue Mac: Impossible to use -splash: with JavaFX 2.2 and JDK 7).

-在 Java 8 之前无法在所有平台上运行(请参阅问题Mac:无法使用 -splash:与 JavaFX 2.2 和 JDK 7)。

2. Preloaders may be used for standalone applications.

2. 预加载器可用于独立应用程序。

The JavaFX Preloader tutorialhas an example in the section 9.3.4 Using a Preloader to Display the Application Initialization Progress. The tutorial provides executable sample code in the LongInitAppPreloaderand LongInitAppclasses (use the class names I provide in this answer as one name in the tutorial is currently wrong).

JavaFX的预载教程已在部分的例子9.3.4 Using a Preloader to Display the Application Initialization Progress。本教程在LongInitAppPreloaderLongInitApp类中提供了可执行示例代码(使用我在本答案中提供的类名,因为本教程中的一个名称目前是错误的)。

The sample standalone application has a long initialization time and a custom Preloaderprovides feedback on the progress of the initialization. The sample simulates the long initialization through a Taskwith a Thread.sleepcall, but a real application would be doing something like establishing network connections, retrieving and parsing network data and setting up the initial application Scene.

示例独立应用程序具有较长的初始化时间,并且自定义Preloader提供有关初始化进度的反馈。该示例通过带有调用的Task模拟了长时间的初始化Thread.sleep,但实际应用程序将执行诸如建立网络连接、检索和解析网络数据以及设置初始应用程序Scene 之类的操作

Preloaders are not specific to applets and WebStart, but are primarily targeted to those deployment types. The applet and WebStart initialization process is more complex than standalone application initialization, so much of the Preloader documentation is devoted to those more complex scenarios.

预加载器并非特定于小程序和 WebStart,而是主要针对这些部署类型。小程序和 WebStart 的初始化过程比独立的应用程序初始化更复杂,因此 Preloader 文档的大部分内容都专门用于那些更复杂的场景。

3. You don't need to place a Preloader in a separate JAR.

3. 不需要在单独的 JAR 中放置 Preloader。

You can place the Preloaderin the same JAR as your Applicationclass. For large applications dynamically deployed and updated over network loading protocols such as WebStart, placing the Preloaderin a seperate JAR makes sense. For standalone applications performing network based initialization, it probably doesn't make much difference and the separate packaging step could be skipped to simplify the build and deployment process.

您可以将它Preloader与您的应用程序类放在同一个 JAR 中。对于通过网络加载协议(例如WebStart )动态部署和更新的大型应用程序,将其放置Preloader在单独的 JAR 中是有意义的。对于执行基于网络的初始化的独立应用程序,它可能没有太大区别,可以跳过单独的打包步骤以简化构建和部署过程。

4. You can achieve Preloader style functionality without using a Preloader.

4. 您可以在不使用 Preloader 的情况下实现 Preloader 风格的功能。

Much (not all) of the Preloader functionality can be achieved without subclassing Preloader.

许多(不是全部) Preloader 功能可以在不继承 Preloader 的情况下实现。

You can:

你可以:

  1. Create a startup Stagein your application's startmethod.
  2. Place a splash image and ProgressBarin the startup stage.
  3. Have a background taskfor lengthy application initialization processes.
  4. Report initialization progress back to your startup stage from your background task.
  5. On initialization completion, either:
    • a. Replace the startup stage with a newly created application stage OR
    • b. Replace the contents of the scene in the startup stage with a new scene for your application.
  1. 在应用程序的start方法中创建一个启动阶段
  2. 在启动阶段放置一个启动画面和ProgressBar
  3. 为冗长的应用程序初始化过程设置后台任务
  4. 从您的后台任务将初始化进度报告回您的启动阶段。
  5. 在初始化完成时,要么:
    • 一种。用新创建的应用程序阶段替换启动阶段或
    • 湾 将启动阶段的场景内容替换为应用程序的新场景。

5b is probably preferred so that you don't need to create multiple windows.

5b 可能是首选,这样您就不需要创建多个窗口。

For examples of this strategy, see my answers to the following questions:

有关此策略的示例,请参阅我对以下问题的回答:

The related sample code for displaying Progress Monitoring splash screens in JavaFX without using a Preloaderis:

在 JavaFX 中不使用 a 显示进度监控闪屏的相关示例代码Preloader是:

The above code could be refactored to use a Preloader subclass instead, in which case there is a well defined framework for notification of application initialization events and more flexible deployment models (e.g. preloader in seperate jar) are available. However use of a Preloadermay be a little complicated. For some implementations, it may not be worth the time to understand the Preloaderframework.

上面的代码可以重构为使用 Preloader 子类来代替,在这种情况下,有一个明确定义的框架来通知应用程序初始化事件,并且可以使用更灵活的部署模型(例如单独 jar 中的预加载器)。但是 a 的使用Preloader可能有点复杂。对于某些实现,可能不值得花时间了解Preloader框架。

5. WebStart Apps have JNLP support for Splash Images

5. WebStart 应用程序具有对 Splash 图像的 JNLP 支持

(this point is pretty irrelevant and just included for completeness).

(这一点非常无关紧要,只是为了完整性而包括在内)。

I believe that webstart applications can have a flag in their jnlp fileto show the startup image as the webstart application launches, but I've never been able to get that flag to work in a JavaFX 2 application, only in a Swing application and even then it wasn't all that reliable as it would only display the second time the application was launched.

我相信webstart 应用程序可以在他们的 jnlp 文件中有一个标志来在 webstart 应用程序启动时显示启动图像,但我从来没有能够让这个标志在 JavaFX 2 应用程序中工作,只有在 Swing 应用程序中,甚至那么它并不是那么可靠,因为它只会在应用程序第二次启动时显示。

回答by tomsontom

IMHO a Preloader only makes sense when you are running as an applet or webstart because the preloader can be packaged as an extra Jar which is downloaded first and executed while the rest of your application is downloaded in the background.

恕我直言,预加载器仅在您作为小程序或 webstart 运行时才有意义,因为预加载器可以打包为一个额外的 Jar,首先下载并执行,而应用程序的其余部分在后台下载。

So my suggestion would be to open a stage at the first point in time when you get a Stage and e.g. display a splash.

所以我的建议是在你获得舞台的第一个时间点打开一个舞台,例如显示一个飞溅。