2013-04-01 78 views
1

我想創造一些負載點是這樣的:的JavaFX 2動態加載點

At 0 second the text on the screen is: Loading. 
At 1 second the text on the screen is: Loading.. 
At 2 second the text on the screen is: Loading... 
At 3 second the text on the screen is: Loading. 
At 4 second the text on the screen is: Loading.. 
At 5 second the text on the screen is: Loading... 

依此類推,直到我關閉Stage

JavaFX中最好的/最簡單的方法是什麼?我一直在研究JavaFX中的動畫/預加載器,但在嘗試實現這一點時似乎很複雜。

我一直在試圖建立之間的循環這三個Text

Text dot = new Text("Loading."); 
Text dotdot = new Text("Loading.."); 
Text dotdotdot = new Text("Loading..."); 

但屏幕保持靜態...

我怎樣才能使這項工作正確的JavaFX?謝謝。

回答

4

這個問題是相似的:javafx animation looping

這是一個使用JavaFX animation framework的解決方案 - 它看起來非常直截了當,並且不太複雜。

loading animation

import javafx.animation.*; 
import javafx.application.Application; 
import javafx.event.*; 
import javafx.scene.*; 
import javafx.scene.control.Label; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

/** Simple Loading Text Animation. */ 
public class DotLoader extends Application { 
    @Override public void start(final Stage stage) throws Exception { 
    final Label status = new Label("Loading"); 
    final Timeline timeline = new Timeline(
     new KeyFrame(Duration.ZERO, new EventHandler() { 
     @Override public void handle(Event event) { 
      String statusText = status.getText(); 
      status.setText(
      ("Loading . . .".equals(statusText)) 
       ? "Loading ." 
       : statusText + " ." 
     ); 
     } 
     }), 
     new KeyFrame(Duration.millis(1000)) 
    ); 
    timeline.setCycleCount(Timeline.INDEFINITE); 

    VBox layout = new VBox(); 
    layout.getChildren().addAll(status); 
    layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;"); 
    stage.setScene(new Scene(layout, 50, 35)); 
    stage.show(); 

    timeline.play(); 
    } 

    public static void main(String[] args) throws Exception { launch(args); } 
} 
+0

非常有幫助,謝謝! –

0

您是否考慮過使用進度指示器或進度條?我認爲他們可以是一個很好的解決方案來顯示動畫並避免問題。

我已經能夠在JavaFX中完成它,而不是使用動畫,而是使用JavaFX中的併發類。

我讓你在這裏的代碼in a gist。我認爲這不是很直觀,因爲我更喜歡進度指標。也許這不是最好的解決方案,但也許這會幫助你。

乾杯