今天分享使用java FX中的Progress bar,原想讓程式執行後,利用迴圈progress bar會慢慢從0到100%,於是就心想實作runnable,讓值慢慢增加,但結果是一次就跑出100%,所以要產生這樣的效果不是這樣用的,原來JavaFX裡面有定義給FX元件用的Concurrency Framework,今天已經來不急研讀了,明天將Pro JavaFX 2中的範例消化後,再跟大家分享,以下是個人錯誤的code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package myfx5;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.control.Label;
/**
*
* @author joombuopre
*/
public class MyFX5 extends Application implements Runnable {
ProgressBar pb= new ProgressBar();
SimpleDoubleProperty i= new SimpleDoubleProperty(0);
@Override
public void start(Stage primaryStage) {
Label l1=new Label("Progress");
pb.setMinWidth(200);
HBox root = new HBox();
root.setPadding(new Insets(10,0,0,10));
root.setSpacing(10);
root.getChildren().addAll(l1,pb);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("ProgressBar Test");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
@Override
public void run() {
while(i.get()<1){
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(MyFX5.class.getName()).log(Level.SEVERE, null, ex);
}
pb.setProgress(i);
System.out.println(i); //0.1, 0.2, 0.3....
}
}
}