gpt4 book ai didi

java - 使用 JavaFX 在文本上“键入”动画

转载 作者:行者123 更新时间:2023-11-29 03:03:08 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com