當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


JavaFX 類 ProgressIndicator用法及代碼示例

ProgressIndicator是JavaFX軟件包的一部分。這是一個圓形控件,用於指示進度(無限或有限)。通常與Task API配合使用,以表示後台Task的進度。它通常顯示任務的完成量。

該類的構造函數是

  1. ProgressIndicator():創建一個新的中間進度指示器。
  2. ProgressIndicator(double p):創建具有指定進度的進度指示器

常用方法


方法 說明
isIndeterminate() 獲取不確定的屬性的值。
getProgress() 獲取屬性進度的值。
setProgress(double v) 設置屬性進度的值

以下示例程序旨在說明進度指示器的用法:

創建進度指示器的程序:該程序將創建一個進度指示器,名稱為pb。進度指示器將在場景內創建,而場景又將托管在舞台內。函數setTitle()用於為舞台提供標題。然後,創建一個平鋪窗格,在其上調用addChildren()方法以將進度指示器和按鈕附加到場景內部。最後,調用show()方法以顯示最終結果。

// Java program to illustrate the use of Progress Indicator 
import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.*; 
import javafx.scene.layout.*; 
import java.io.*; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.scene.control.Label; 
import javafx.stage.Stage; 
import java.net.*; 
public class progressi extends Application { 
  
    static double ii = 0; 
  
    // launch the application 
    public void start(Stage s) throws Exception 
    { 
        // set title for the stage 
        s.setTitle("creating progressIndicator"); 
  
        // create a progress indicator 
        ProgressIndicator pb = new ProgressIndicator(); 
  
        // create a tile pane 
        TilePane r = new TilePane(); 
  
        // action event 
        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() { 
            public void handle(ActionEvent e) 
            { 
                // set progress to different level of progressindicator 
                ii += 0.1; 
                pb.setProgress(ii); 
            } 
  
        }; 
  
        // creating button 
        Button b = new Button("increase"); 
  
        // set on action 
        b.setOnAction(event); 
  
        // add button 
        r.getChildren().add(pb); 
        r.getChildren().add(b); 
  
        // create a scene 
        Scene sc = new Scene(r, 200, 200); 
  
        // set the scene 
        s.setScene(sc); 
  
        s.show(); 
    } 
  
    public static void main(String args[]) 
    { 
        // launch the application 
        launch(args); 
    } 
}

輸出:

注意:以下程序可能無法在在線IDE中運行,請使用離線編譯器。
參考:https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ProgressIndicator.html



相關用法


注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 JavaFX | ProgressIndicator。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。