JavaFX打字机对标签的影响

时间:2014-11-27 18:39:56

标签: javafx-2 javafx-8

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

 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();

}

2 个答案:

答案 0 :(得分: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();

}

答案 1 :(得分:0)

我不知道这是否是您想要实现的效果,但我已经创建了(丑陋)演示如何使用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();
}
}