# 显示自定义加载进度指示器

> 原文： [https://docs.oracle.com/javase/tutorial/deployment/webstart/customProgressIndicatorForAppln.html](https://docs.oracle.com/javase/tutorial/deployment/webstart/customProgressIndicatorForAppln.html)

Java Web Start 应用程序可以显示自定义加载进度指示器，该指示器显示应用程序资源的下载进度。

考虑 Weather 应用程序和`CustomProgress`类，以了解如何为 Java Web Start 应用程序实现自定义加载进度指示器。为了演示大量且长时间的下载，此 Java Web Start 应用程序的 JAR 文件已被人为地夸大，`customprogress_webstart.jnlp`文件将其他 JAR 文件指定为资源。

## 开发定制的加载进度指标

要为 Java Web Start 应用程序开发自定义加载进度指示器，请创建一个实现 [`DownloadServiceListener`](https://docs.oracle.com/javase/8/docs/jre/api/javaws/jnlp/javax/jnlp/DownloadServiceListener.html) 接口的类。

加载进度指示器类的构造函数不应具有任何参数。

```
import javax.jnlp.DownloadServiceListener;
import java.awt.Container;
import java.applet.AppletStub;
import netscape.javascript.*;
// ...
public class CustomProgress
        implements DownloadServiceListener {   
    JFrame frame = null;
    JProgressBar progressBar = null;
    boolean uiCreated = false;

    public CustomProgress() {
    }
...
}    

```

以下代码段显示了如何为加载进度指示器构建 UI：

```
private void create() {
    JPanel top = createComponents();
    frame = new JFrame(); // top level custom progress
                          // indicator UI
    frame.getContentPane().add(top,
                               BorderLayout.CENTER);
    frame.setBounds(300,300,400,300);
    frame.pack();
    updateProgressUI(0);
}

private JPanel createComponents() {
    JPanel top = new JPanel();
    top.setBackground(Color.WHITE);
    top.setLayout(new BorderLayout(20, 20));

    String lblText =
        "<html><font color=green size=+2" +
        ">JDK Documentation</font><br/> " +
        "The one-stop shop for Java enlightenment! <br/></html>";
    JLabel lbl = new JLabel(lblText);
    top.add(lbl, BorderLayout.NORTH);
    ...
    progressBar = new JProgressBar(0, 100);
    progressBar.setValue(0);
    progressBar.setStringPainted(true);
    top.add(progressBar, BorderLayout.SOUTH);

    return top;
}

```

根据`overallPercent`参数，在以下方法中创建和更新加载进度指示器。 Java Web Start 软件定期调用这些方法来传达应用程序下载的进度。当资源下载和验证 100％完成时，Java Web Start 软件将始终发送消息。

```
public void progress(URL url, String version, long readSoFar,
                     long total, int overallPercent) {        
    updateProgressUI(overallPercent);

}

public void upgradingArchive(java.net.URL url,
                  java.lang.String version,
                  int patchPercent,
                  int overallPercent) {
    updateProgressUI(overallPercent);
}

public void validating(java.net.URL url,
            java.lang.String version,
            long entry,
            long total,
            int overallPercent) {
    updateProgressUI(overallPercent);
}

private void updateProgressUI(int overallPercent) {
    if (overallPercent > 0 && overallPercent < 99) {
        if (!uiCreated) {
            uiCreated = true;
            // create custom progress indicator's
            // UI only if there is more work to do,
            // meaning overallPercent > 0 and
            // < 100 this prevents flashing when
            // RIA is loaded from cache
            create();
        }
        progressBar.setValue(overallPercent);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                frame.setVisible(true);
            }
        });
    } else {
        // hide frame when overallPercent is
        // above 99
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                frame.setVisible(false);
                frame.dispose();
            }
        });
    }
}

```

编译加载进度指示器类并使用显示加载进度指示器所需的所有资源构建 JAR 文件。在`classpath`中包含`&lt;your JRE directory&gt;/lib/javaws.jar`文件以启用编译。

加载进度指示器类现在可以使用了。下一步是将此加载进度指示器 JAR 文件指定为 Java Web Start 应用程序的进度指示器。

## 为 Java Web Start 应用程序指定自定义加载进度指示器

要为 Java Web Start 应用程序指定自定义加载进度指示器，请在应用程序的 JNLP 文件中包含以下信息：

*   带有`download="progress"`属性的`jar`标签
*   `progress-class`属性，带有自定义加载进度类的完全限定名称。

来自 [``customprogress_webstart.jnlp``](https://docs.oracle.com/javase/tutorialJWS/samples/deployment/customprogress_webstartJWSProject/customprogress_webstart.jnlp)文件的以下代码片段显示`download="progress"`和`progress-class`属性的用法。

```
<jnlp spec="1.0+" codebase=
  "https://docs.oracle.com/javase/tutorialJWS/samples/deployment" 
   href="customprogress_webstartJWSProject/customprogress_webstart.jnlp">
  <!-- ... -->
  <resources>
    <j2se version="1.7+"/>
    <jar href=
      "webstart_AppWithCustomProgressIndicator/webstart_AppWithCustomProgressIndicator.jar" />
    <jar href=
      "webstart_CustomProgressIndicator/webstart_CustomProgressIndicator.jar"
         download="progress" />
    <jar href=
      "webstart_AppWithCustomProgressIndicator/lib/IconDemo.jar" />
    <jar href=
      "webstart_AppWithCustomProgressIndicator/lib/SplitPaneDemo.jar" />
    <jar href=
      "webstart_AppWithCustomProgressIndicator/lib/SplitPaneDemo2.jar" />
    <jar href=
      "webstart_AppWithCustomProgressIndicator/lib/TextBatchPrintingDemo.jar" />
    <jar href=
      "webstart_AppWithCustomProgressIndicator/lib/ToolBarDemo.jar" />
    <jar href=
      "webstart_AppWithCustomProgressIndicator/lib/ToolBarDemo2.jar" />
    <jar href=
      "webstart_AppWithCustomProgressIndicator/lib/SwingSet2.jar" />
  </resources>
  <application-desc 
      main-class="customprogressindicatordemo.Main"
      progress-class="customprogressindicator.CustomProgress"
  />
  <!-- ... -->
</jnlp>

```

在 Web 页面中部署 Java Web Start 应用程序。在 Web 浏览器中打开 [``JavaWebStartAppPage.html``](examples/dist/webstart_AppWithCustomProgressIndicator/JavaWebStartAppPage.html)，以查看 Weather 应用程序的自定义加载进度指示器。

* * *

**Note:**  To view the example properly, you need to install at least the [Java SE Development Kit (JDK) 6 update 18](http://www.oracle.com/technetwork/java/javase/downloads/index.html) release.

* * *

* * *

**Note:** If you have viewed this Java Web Start application before, clear your cache by using the Java Control Panel before viewing the application again. You will not be able to see a progress indicator for a previously cached application.

* * *

* * *

**Note:**  If you don't see the example running, you might need to enable the JavaScript interpreter in your browser so that the Deployment Toolkit script can function properly.

* * *

[下载 _Java Web Start 应用程序的源代码 _](examplesIndex.html#AppWithCustomProgressIndicator)，其中包含自定义加载进度指示器示例，以进一步进行实验。

有关自定义富 Internet 应用程序（RIA）加载体验的详细信息，请参阅[自定义加载体验](../doingMoreWithRIA/customizeRIALoadingExperience.html)主题。