Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

ProgressIndicator 及其直接子类 

ProgressBar 提供了指示特定任务正在运行并检测其完成进度的能力。 不过

ProgressBar 类用来显示一个显示进度完成的条,而 ProgressIndicator 类则是将进度动态地显示在一个饼图里。见 

Figure 16-1 .

Figure 16-1 Progress Bar and Progress Indicator


 
 

Description of "Figure 16-1 Progress Bar and Progress Indicator"

创建进度控件
 Example 16-1 中的代码能够在 JavaFX 应用中插入一个进度控件。

Example 16-1 Implementing the Progress Bar and Progress Indicator


Java 代码  

1. ProgressBar pb = new ProgressBar(0.6);  

2. ProgressIndicator pi = new ProgressIndicator(0.6);   

也可以使用空构造方法创建进度控件而不指定参数。这时候,可以使用 setProgress 方法为它分配值。另一个


初始化进度控件的方法是使用 ProgressBarBuilder 类,该类包括诸如 build 和 progress 之类的方法。
可以查看 API 文档去了解更多。

有时候应用并不能缺地 in 个任务的完成时间,这时进度控件就保持在非确定模式中直到可以确定。Figure
16-2 中是依赖于不同进度变量值的进度控件。
 

Figure 16-2 Various States of Progress Controls

Description of "Figure 16-2 Various States of Progress Controls" 

Example 16-2   shows the source code of the application shown in  Figure 16-2 .

Example 16-2 Enabling Different States of Progress Controls


Java 代码  

1. import javafx.application.Application;   
2. import javafx.geometry.Pos;   

3. import javafx.scene.Group;   

4. import javafx.scene.Scene;   

5. import javafx.scene.control.Label;  

6. import javafx.scene.control.ProgressBar;   

7. import javafx.scene.control.ProgressIndicator;   

8. import javafx.scene.layout.HBox;   

9. import javafx.scene.layout.VBox;   

10. import javafx.stage.Stage;   

11.   

12. public class Main extends Application   

13. { final Float[] values = new Float[] {-1.0f, 0f, 0.6f, 1.0f};  

14.  final Label [] labels = new Label[values.length];  

15.  final ProgressBar[] pbs = new ProgressBar[values.length];  

16.  final ProgressIndicator[] pins = new ProgressIndicator[values.length];  

17.  final HBox hbs [] = new HBox [values.length];   

18. @Override  

19.  public void start(Stage stage)   

20. { Group root = new Group();   

21. Scene scene = new Scene(root, 300, 150);   

22. scene.getStylesheets().add("progresssample/Style.css");   

23. stage.setScene(scene);   

24. stage.setTitle("Progress Controls");  

25.  for (int i = 0; i < values.length; i++)   

26. { final Label label = labels[i] = new Label();   

27. label.setText("progress:" + values[i]);   

28. final ProgressBar pb = pbs[i] = new ProgressBar();   

29. pb.setProgress(values[i]);   

30. final ProgressIndicator pin = pins[i] = new ProgressIndicator();  

31.  pin.setProgress(values[i]);  

32.  final HBox hb = hbs[i] = new HBox();   

33. hb.setSpacing(5);  

34.  hb.setAlignment(Pos.CENTER);   

35. hb.getChildren().addAll(label, pb, pin);  

36.  }   

37. final VBox vb = new VBox();   

38. vb.setSpacing(5);   

39. vb.getChildren().addAll(hbs);   

40. scene.setRoot(vb);  

41.  stage.show();   

42. }   

43. public static void main(String[] args)   

44. { launch(args); }  

45.  }   
 

一个在 0 和 1 之间的正数用来指示进程的百分比。比如,0.4 代表 40%。而一个负数表示进度在非确定模式。


用方法 isIndeterminate 可以检查进度控件是否在非确定模式中。

 
在界面上指示进度
Figure 16-2 曾经简单的显示了进度控件的所以可能状态。实际应用中,进度值可以通过其他 UI 元素的值
获得。

研究 Example 16-3 中的代码学习如何为基于滑标位置的进度条和指示器设置值。

Example 16-3 Receiving the Progress Value from a Slider


import javafx.application.Application; import
javafx.beans.value.ChangeListener; import
javafx.beans.value.ObservableValue; import javafx.geometry.Pos; import
javafx.scene.Group; import javafx.scene.Scene; import
javafx.scene.control.ProgressBar; import
javafx.scene.control.ProgressIndicator; import javafx.scene.control.Slider;
import javafx.scene.layout.HBox; import javafx.stage.Stage; public class
Main extends Application { @Override public void start(Stage stage) { Group
root = new Group(); Scene scene = new Scene(root); stage.setScene(scene);
stage.setTitle("Progress Controls"); final Slider slider = new Slider();
slider.setMin(0); slider.setMax(50); final ProgressBar pb = new
ProgressBar(0); final ProgressIndicator pi = new ProgressIndicator(0);
slider.valueProperty().addListener(new ChangeListener<Number>() { public
void changed(ObservableValue<? extends Number> ov, Number old_val, Number
new_val) { pb.setProgress(new_val.doubleValue()/50);
pi.setProgress(new_val.doubleValue()/50); } }); final HBox hb = new HBox();
hb.setSpacing(5); hb.setAlignment(Pos.CENTER);
hb.getChildren().addAll(slider, pb, pi); scene.setRoot(hb); stage.show(); }
public static void main(String[] args) { launch(args); } }

编译运行效果见 Figure 16-3 .

Figure 16-3 Indicating the Progress Set by a Slider

Description of "Figure 16-3 Indicating the Progress Set by a Slider" 

一个 ChangeListener 对象决定了是否滑动条在动并计算进度条和指示器的值,所以进度控件值的范围
是 0.0 到 1.0.

You might also like