2013-04-01 43 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。我认为这不是很直观,因为我更喜欢进度指标。也许这不是最好的解决方案,但也许这会帮助你。

干杯