Javafx 2 标签的JavaFX打字机效果
我对这种方法有些问题。它工作正常,但有一个小问题。我称之为这种方法的时间太少了。因此,标签上只打印最后一个字符串。但我希望下一个字符串开始打印,只有在上一个字符串完成之后。 对不起,我的英语不好((Javafx 2 标签的JavaFX打字机效果,javafx-2,javafx-8,Javafx 2,Javafx 8,我对这种方法有些问题。它工作正常,但有一个小问题。我称之为这种方法的时间太少了。因此,标签上只打印最后一个字符串。但我希望下一个字符串开始打印,只有在上一个字符串完成之后。 对不起,我的英语不好(( 我不知道这是否是您试图实现的效果,但我已经创建了(丑陋的)演示,演示了如何使用时间线来实现这一点 public class Main extends Application { public static void main(String[] args) { launch(args); }
我不知道这是否是您试图实现的效果,但我已经创建了(丑陋的)演示,演示了如何使用
时间线来实现这一点
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();
}
}
使用以下代码获得打字效果
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();
}
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();
}