您的位置:首页 > 编程语言 > Java开发

(翻译)第二十二回 JavaFX2.0 进度条和进度指示器

2011-11-02 00:35 423 查看
原文地址http://download.oracle.com/javafx/2.0/ui_controls/progress.htm

 

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代码  



ProgressBar pb = new ProgressBar(0.6);  

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代码  



import javafx.application.Application;   

import javafx.geometry.Pos;   

import javafx.scene.Group;   

import javafx.scene.Scene;   

import javafx.scene.control.Label;  

import javafx.scene.control.ProgressBar;   

import javafx.scene.control.ProgressIndicator;   

import javafx.scene.layout.HBox;   

import javafx.scene.layout.VBox;   

import javafx.stage.Stage;   

  

public class Main extends Application   

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

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

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

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

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

@Override  

 public void start(Stage stage)   

{ Group root = new Group();   

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

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

stage.setScene(scene);   

stage.setTitle("Progress Controls");  

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

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

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

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

pb.setProgress(values[i]);   

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

 pin.setProgress(values[i]);  

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

hb.setSpacing(5);  

 hb.setAlignment(Pos.CENTER);   

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

 }   

final VBox vb = new VBox();   

vb.setSpacing(5);   

vb.getChildren().addAll(hbs);   

scene.setRoot(vb);  

 stage.show();   

}   

public static void main(String[] args)   

{ launch(args); }  

 }   

 

一个在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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息