简体   繁体   English

如何仅更改 javafx TextField 中最后一个字符的颜色

[英]How to only change color of the last character in a javafx TextField

I am trying to make a typing game, I created a text field for user input and am trying to make the game so that the last char of the textfield is red if it doesn't match the corresponding char in the words to be typed.我正在尝试制作一个打字游戏,我为用户输入创建了一个文本字段,并尝试制作游戏,以便如果文本字段的最后一个字符与要键入的单词中的相应字符不匹配,则它是红色的。 Below is what I have so far along with the current types of relevant nodes.以下是我目前拥有的相关节点类型。

Label wordsToType = new Label();
TextField userInput = new TextField();
StringBuilder wordsTyped = new StringBuilder();
TextField lastChar = new TextField();

userInput.setOnKeyReleased(e -> {
    wordsTyped.append(userInput.getText()); // words typed is a string builder of what is in userInput

    if (wordsTyped.toString().charAt(wordsTyped.length() - 1) - wordsToType.getText().charAt(wordsTyped.length() - 1) != 0) {
        lastChar.setText(e.getText());
        lastChar.setStyle("-fx-text-fill: red; -fx-font-size: 16px;");
        wordsTyped.setLength(wordsTyped.length() - 1); // deletes last char from wordsTyped
        userInput.setText(wordsTyped.toString() + lastChar.getText());
        userInput.end();
    }
    wordsTyped.delete(0, wordsTyped.length());
});

As mentioned in the comments, you can use RichTextFX to highlight input texts with different styles.正如评论中提到的,您可以使用RichTextFX突出显示具有不同 styles 的输入文本。 Here is a sample code:这是一个示例代码:

Java: Java:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.fxmisc.richtext.StyleClassedTextField;
import org.fxmisc.richtext.StyledTextField;
import org.fxmisc.richtext.model.StyleSpans;
import org.fxmisc.richtext.model.StyleSpansBuilder;

import java.util.Collection;
import java.util.Collections;

public class HighlightedTextFieldExample extends Application {

    @Override
    public void start(Stage stage) {
        StyledTextField<Collection<String>, Collection<String>> textField = new StyleClassedTextField();
        textField.lengthProperty().addListener((observableValue, oldValue, newValue) -> {
            if (newValue > 0) {
                StyleSpans<Collection<String>> styleSpans = new StyleSpansBuilder<Collection<String>>()
                        .add(Collections.emptyList(), newValue - 1) // no style (leading characters)
                        .add(Collections.singleton("last-character"), 1) // add a style class for the last character
                        .create();
                textField.setStyleSpans(0, styleSpans);
            }
        });
        textField.setPrefWidth(400);

        Scene scene = new Scene(textField);
        scene.getStylesheets().add(HighlightedTextFieldExample.class.getResource("highlighting.css").toExternalForm());
        stage.setScene(scene);
        stage.setTitle("Last Character Highlighting Example");
        stage.show();
    }

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

CSS Style File ( highlighter.css ): CSS 样式文件( highlighter.css 8CBA22E28EB17B5F5C6AE2A266AZ):

.last-character {
    -fx-fill: red;
}

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM