【问题标题】:JavaFX typewriter effect for label标签的JavaFX打字机效果
【发布时间】:2014-11-27 18:39:55
【问题描述】:

我对这种方法有一些问题。它工作正常,但有一个小问题。我调用这个方法的时间太少了。所以只有最后一个字符串打印在标签上。但我希望下一个字符串开始打印,只有在前一个字符串完成之后。 对不起我的英语((

 public void some(final String s) {

    final Animation animation = new Transition() {
        {
            setCycleDuration(Duration.millis(2000));
        }

        protected void interpolate(double frac) {


            final int length = s.length();
            final int n = Math.round(length * (float) frac);
            javafx.application.Platform.runLater(new Runnable() {
                @Override
                public void run() {

                    status.setValue(s.substring(0, n));

                }
            }
            );
        }

    };

    animation.play();

}

【问题讨论】:

    标签: javafx-2 javafx-8


    【解决方案1】:

    使用下面的代码可以获得打字效果。

    public void AnimateText(Label lbl, String descImp) {
        String content = descImp;
        final Animation animation = new Transition() {
            {
                setCycleDuration(Duration.millis(2000));
            }
    
            protected void interpolate(double frac) {
                final int length = content.length();
                final int n = Math.round(length * (float) frac);
                lbl.setText(content.substring(0, n));
            }
        };
        animation.play();
    
    }
    

    【讨论】:

      【解决方案2】:

      我不知道这是否是您想要达到的效果,但我创建了(丑陋的)演示,您如何使用TimeLine 做到这一点

      public class Main extends Application {
      
      public static void main(String[] args) {
          launch(args);
      }
      
      @Override
      public void start(Stage primaryStage) throws Exception {
          IntegerProperty letters= new SimpleIntegerProperty(0);
          Label label = new Label();
          Button animate = new Button("animate");
          letters.addListener((a, b, c) -> {
              label.setText("animate".substring(0, c.intValue()));
          });
      
          animate.setOnAction((e)->{
              Timeline timeline = new Timeline();
              KeyValue kv = new KeyValue(letters, "animate".length());
              KeyFrame kf = new KeyFrame(Duration.seconds(3), kv);
              timeline.getKeyFrames().add(kf);
              timeline.play();
          });
          BorderPane pane = new BorderPane(label, null, null, animate, null);
      
          primaryStage.setScene(new Scene(pane, 300,300));
          primaryStage.show();
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多