JavaFX - 進度指示器



一個進度指示器是一個UI控制元件,用於向用戶指示任務的進度。它通常與Task API一起使用,以通知使用者後臺任務的進度以及完成使用者操作需要多長時間。在本教程中,我們將學習如何在JavaFX中建立和使用進度指示器。在此之前,讓我們看看一般的進度指示器是什麼樣的:

Sample progress indicator

JavaFX中的進度指示器

在JavaFX中,進度指示器由名為ProgressIndicator的類表示。此類屬於包javafx.scene.control。透過例項化此類,我們可以在JavaFX中建立一個進度指示器。ProgressIndicator類的建構函式列在下面:

  • ProgressIndicator() - 它構造一個沒有初始進度值的新進度指示器。

  • ProgressIndicator(double progress) - 它構造一個具有指定初始進度值的新進度指示器。

示例

以下程式演示如何在JavaFX中建立一個進度指示器。將此程式碼儲存在名為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檔案,請使用以下命令:

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

輸出

當我們執行上述程式碼時,它將生成以下輸出。

ProgressIndicator Output

具有初始進度值的進度指示器

我們可以使用ProgressIndicator類的引數化建構函式將初始進度值設定為進度指示器。建構函式接受一個進度值作為引數,該引數是介於0.0和1.0之間的雙精度型別。這裡,0.0表示沒有進度,1.0表示完成。如果進度值為負數,則進度指示器是不確定的,這意味著它不顯示任何特定的進度量。

示例

在下面的示例中,我們建立了一個用特定進度值初始化的進度指示器。此外,我們還建立了兩個分別標記為“增加”和“減少”的按鈕。“增加”按鈕單擊後會增加進度值,“減少”按鈕單擊後會減少進度值。將此程式碼儲存在名為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檔案:

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

輸出

執行上述程式碼後,將生成以下輸出。

ProgressIndicator Output2
廣告
© . All rights reserved.