java - 使用 JavaFX 在文本上“打字”动画

标签 java javafx

所以我尝试起草一种方法,该方法需要一个字符串和一个文本,理论上会逐渐将字符串输出到该文本,就像有人正在输入它一样。然而,所发生的只是程序暂停不同的时间(由于每次添加字母之间的暂停使用了随机值),然后显示整个字符串。

public void keyboard(String string, Text tBox, Stage stage, Scene scene) {
    //generates a random number which later determines the amount of time the game pauses before
    //adding the next character
    Random number = new Random();

    //stores the length of a string (as an integer)
    int stringLen = string.length();

    //gets increased later
    int counter = 0;

    char[] chars = new char[(stringLen+1)];
    string.getChars(0, stringLen, chars, 0);

    String returnVal = "";

    do {
        returnVal += Character.toString(chars[counter]);
        counter += 1;
        tBox.setText(returnVal);
        pause((number.nextInt(3) + 1) * 100);
        stage.setScene(scene);
        stage.show();
    } while (returnVal != string);

}

暂停方法如下。

private void pause(int milliseconds) {
    try {
        Thread.sleep(milliseconds);
    } catch (InterruptedException err) {
        System.err.println("Interrupted during pause.");
        System.exit(0);
    }
}

请暂停。

编辑:

public void keyboard(String string, Text tBox) {
    final IntegerProperty counter = new SimpleIntegerProperty(0);
    Random number = new Random();
    int length = string.length();
    tBox.setFill(Color.WHITE);

    Timeline line = new Timeline();
    KeyFrame frame = new KeyFrame(Duration.seconds((number.nextInt(3) + 1) / 10),
        event -> { 
            if(counter.get() > length) {
                line.stop();
            } else {
                tBox.setText(string.substring(0, counter.get()));
                counter.set(counter.get() + 1);
            }
        });

    line.getKeyFrames().add(frame);
    line.setCycleCount(Animation.INDEFINITE);
    line.play();
}

仅供引用,当您在 TextField 中键入内容时,键盘方法就会启动,然后检查输入是否有效,并据此启动动画。

最佳答案

当您调用 Thread.sleep() 时,您正在阻塞 JavaFX 应用程序线程这不是正确的做法。

如果您需要动画,您应该使用 Timeline 反而。

Timeline processes individual KeyFrame at or after specified time interval elapsed

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application {
    private final String str ="Itachi";

    @Override
    public void start(Stage primaryStage) {
        Text text = new Text();
        VBox root = new VBox(text);
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 330, 120, Color.WHITE);
        primaryStage.setScene(scene);
        primaryStage.show();

        final IntegerProperty i = new SimpleIntegerProperty(0);
        Timeline timeline = new Timeline();
        KeyFrame keyFrame = new KeyFrame(
                Duration.seconds(1),
                event -> {
                    if (i.get() > str.length()) {
                        timeline.stop();
                    } else {
                        text.setText(str.substring(0, i.get()));
                        i.set(i.get() + 1);
                    }
                });
        timeline.getKeyFrames().add(keyFrame);
        timeline.setCycleCount(Animation.INDEFINITE);
        timeline.play();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

关于java - 使用 JavaFX 在文本上“打字”动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33646317/

相关文章:

javafx - 什么是 javafx 2.1 中的警报替换?警报在 javafx 1.3 中但不在 javafx 2.1 中

java - 在 JavaFX 中用 Canvas 绘制笛卡尔平面图

java - 你会使用 DI 还是工厂?

java - 我找不到 org.codehaus.staxmate.dom 包

java - 简单的 Android 应用程序在启动时崩溃

java - 替换除正数/负数以外的所有内容

java - 单击“确定”按钮后如何获取输入?

java - 过渡到 OpenJDK/OpenJFX [多模块 Maven 项目]

java - 使用多线程在 JavaFX 中编辑按钮文本

java - 如何在 JavaFX 中最有效地移动多个矩形