Javafx 简明教程

JavaFX - ProgressIndicator

一个 progress indicator 是一个 UI 控件,用于向用户指示任务的进度。它通常与 Task API 一起使用,以向用户通知后台任务的进度以及完成用户操作所需的时间。在本教程中,我们将学习如何在 JavaFX 中创建和使用进度指示器。在此之前,我们来看看一般进度指示器是什么样子的 −

A progress indicator is a UI control that is used to indicate the progress of a task to the user. It is often used with the Task API to notify users about the progress of background tasks and how much time will be required to complete the user action. In this tutorial, we are going to learn how to create and use progress indicator in JavaFX. Before that, let’s see how a general progress indicator looks like −

progress indicator

ProgressIndicator in JavaFX

在 JavaFX 中,进度指示器由名为 ProgressIndicator 的类表示。此类属于包 javafx.scene.control 。通过实例化此类,我们可以在 JavaFX 中创建一个进度指示器。ProgressIndicator 类的构造函数如下 −

In JavaFX, the progress indicator is represented by a class named ProgressIndicator. This class belongs to the package javafx.scene.control. By instantiating this class, we can create a progress indicator in JavaFX. Constructors of the ProgressIndicator class are listed below −

  1. ProgressIndicator() − It constructs a new progress indicator without initial progress value.

  2. ProgressIndicator(double progress) − It constructs a new progress indicator with a specified initial progress value.

Example

以下程序演示如何在 JavaFX 中创建一个进度指示器。将此代码保存在名为 ProgressindicatorJavafx.java 的文件中。

The following program demonstrates how to create a progress indicator in JavaFX. Save this code in a file with the name ProgressindicatorJavafx.java.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ProgressIndicator;
import javafx.stage.Stage;
public class ProgressindicatorJavafx extends Application {
   @Override
   public void start(Stage stage) {
      // Creating a progress indicator without an initial progress value
      ProgressIndicator progress = new ProgressIndicator();
      // Create a scene and stage
      Scene scene = new Scene(progress, 400, 300);
      stage.setScene(scene);
      stage.setTitle("Progress Indicator in Javafx");
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

要从命令提示符编译并执行已保存的 Java 文件,请使用以下命令−

To compile and execute the saved Java file from the command prompt, use the following commands −

javac --module-path %PATH_TO_FX% --add-modules javafx.controls ProgressindicatorJavafx.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls ProgressindicatorJavafx

当我们执行上述代码时,它将生成以下输出。

When we execute the above code, it will generate the following output.

progress output

ProgressIndicator with an Initial Progress Value

我们可以使用 ProgressIndicator 类的参数化构造函数将初始 progress value 设置为进度指示器。构造函数接受进度值作为参数,该值是介于 0.0 和 1.0 之间的一个双精度类型值。这里,0.0 表示没有进度,1.0 表示完成。如果进度值是负数,则进度指示器是不确定的,这意味着它不显示任何特定进度量。

We can set the initial progress value to the progress indicator by using the parameterized constructor of the ProgressIndicator class. The constructor accepts a progress value as an argument which is of double type ranging between 0.0 and 1.0. Here, 0.0 means no progress and 1.0 means complete. If the progress value is negative, the progress indicator is indeterminate, meaning that it does not show any specific progress amount.

Example

在以下示例中,我们正在创建一个具有特定进度值初始化的进度指示器。此外,我们还创建了两个标记为“增加”和“减少”的按钮。单击“增加”按钮会增加进度值,而单击“减少”则会递减进度值。将此代码保存在名为 Javafxprogress.java 的文件中。

In the following example, we are creating a progress indicator initialized with a specific progress value. Additionally, we are also creating two buttons labelled 'Increase' and 'Decrease'. Clicking the 'Increase' button increments the progress value, while clicking 'Decrease' decrements it. Save this code in a file with the name Javafxprogress.java.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Javafxprogress extends Application {
   @Override
   public void start(Stage stage) {
      // Create a progress indicator with an initial progress of 0.5
      ProgressIndicator progressIndctr = new ProgressIndicator(0.5);
      // Create a button that increases the progress by 0.1
      Button buttonOne = new Button("Increase");
      buttonOne.setOnAction(e -> {
         // Get the current progress value
         double progress = progressIndctr.getProgress();
         // Increase the progress by 0.1
         progress += 0.1;
         // Set the new progress value
         progressIndctr.setProgress(progress);
      });
      // Create second button that decreases the progress by 0.1
      Button buttonTwo = new Button("Decrease");
      buttonTwo.setOnAction(e -> {
         // Get the current progress value
         double progress = progressIndctr.getProgress();
         // Decrease the progress by 0.1
         progress -= 0.1;
         // Set the new progress value
         progressIndctr.setProgress(progress);
      });
      // Create an HBox to hold the progress indicator and the button
      HBox hbox = new HBox(10);
      hbox.getChildren().addAll(progressIndctr,buttonOne, buttonTwo);
      // Create a scene with the HBox and set it to the stage
      Scene scene = new Scene(hbox, 400, 300);
      stage.setScene(scene);
      stage.setTitle("Progress Indicator in JavaFX");
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

通过使用以下命令从命令提示符编译并执行保存的 Java 文件 −

Compile and execute the saved Java file from the command prompt by using the following commands −

javac --module-path %PATH_TO_FX% --add-modules javafx.controls Javafxprogress.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls Javafxprogress

在执行上述代码时,它将生成以下输出。

On executing the above code, it will generate the following output.

progress indicator2