JavaFX 类 ProgressIndicator实例讲解

发布时间:2025-12-09 11:50:08 浏览次数:2

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



progressindicator
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477