Javafx文本多字彩色化 [英] Javafx Text multi-word colorization

查看:342
本文介绍了Javafx文本多字彩色化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有一个ListView对象。我使用这个作为一种控制台窗口为我的服务器。这真的是我能想到的在一个盒子里显示彩色文本的唯一方法。这到目前为止工作奇妙。现在我想要做的是在一个索引或行上颜色不同的文本。



示例:

  listView [0] =Hello 世界; 

其中 Hello 将是绿色的,而 / strong>将是蓝色的。如果这可以使用javafx文本或任何其他方式我想知道如何去做。我使用Javafx Text作为主要罪魁祸首,因为你可以自定义这么多。



我希望每个人都能理解我在这里想做什么,如果不是

$ b
$ b

感谢jewelsea我能够找出一个解决方案。

>

  ListView< FlowPane> consoleWindow = new ListView<>(); 
ArrayList< FlowPane> consoleBuffer = FXCollections.observableArrayList();

consoleWindow.setItems(consoleBuffer);

inputBox.setOnKeyPressed(new EventHandler< KeyEvent>(){
@Override
public void handle(KeyEvent keyEvent){
if(keyEvent.getCode()= = KeyCode.ENTER){
consoleBuffer.add(parseInput.parseInputToArray(inputBox.getText()));
}
consoleWindow.scrollTo(consoleBuffer.size());
}
});

ConsoleInputParse

  public class ConsoleInputParse {

private String [] wordList = {};

public ConsoleInputParse(){}

public FlowPane parseInputToArray(String input){
wordList = input.trim()。split([] +) ;

return colorize();
}

public FlowPane colorize(){

ArrayList< Text> textChunks = new ArrayList<>();
FlowPane bundle = new FlowPane();

// Todo:使用regex检查有效字
for(String word:wordList){
String spaced = word +;
switch(word){
caseHello:casehello:
textChunks.add(customize(spaced,purple));
break;
caseWorld:caseworld:
textChunks.add(customize(spaced,blue));
break;
caseStack Overflow:
textChunks.add(customize(spaced,orange,Arial Bold,15));
默认值:
textChunks.add(customize(spaced,black,Arial,13));
break;
}
}

bundle.getChildren()。addAll(textChunks);
return bundle;
}

public Text customize(String word,String color){
return TextBuilder.create().text(word).fill(Paint.valueOf(color))。建立();
}

public Text customize(String word,String color,String font){
return TextBuilder.create()
.text(word)
.fill(Paint.valueOf(color))
.font(Font.font(font,12))。build();
}

public Text(String word,String color,String font,int fontSize){
return TextBuilder.create()
.text b $ b .fill(Paint.valueOf(color))
.font(Font.font(font,fontSize))。build();
}

}

工作示例

解决方案

创建自定义中,您可以您可以使用 TextFlow 为文本设置样式,而不是在 FlowPane Text $ c>。


Ok, I have a ListView object. I'm using this as a sort of console window for my server. It was really the only way I could think of to display colorized text in a box like that. Which this works wonderful so far. Now what I want to be able to do is color different text on one index or line.

Example:

listView[0] = "Hello " + "world";

Where "Hello" would be green and "world" would be blue. If this can be done usine javafx Text or any other way I would like to know how to go about it. I'm using Javafx Text as the primary culprit since you can customize so much with it.

I hope everyone can understand what I'm trying to do here, if not, let me know and I'll try to reword it a bit.

SOLUTION

Thanks to jewelsea I was able to figure out a solution. I went with a bit of a different approach with it instead of using a cellfactory.

ListView

ListView<FlowPane> consoleWindow = new ListView<>();
ArrayList<FlowPane> consoleBuffer = FXCollections.observableArrayList();

consoleWindow.setItems(consoleBuffer);

inputBox.setOnKeyPressed(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent keyEvent) {
           if (keyEvent.getCode() == KeyCode.ENTER) {
                consoleBuffer.add(parseInput.parseInputToArray(inputBox.getText()));
           }
            consoleWindow.scrollTo(consoleBuffer.size());
        }
});

ConsoleInputParse:

public class ConsoleInputParse {

private String[] wordList = {};

public ConsoleInputParse() {}

public FlowPane parseInputToArray(String input) {
    wordList = input.trim().split("[ ]+");

    return colorize();
}

public FlowPane colorize() {

    ArrayList<Text> textChunks = new ArrayList<>();
    FlowPane bundle = new FlowPane();

    //Todo: use regex to check for valid words
    for (String word : wordList) {
        String spaced = word + " ";
        switch (word) {
            case "Hello": case "hello":
                textChunks.add(customize(spaced, "purple"));
                break;
            case "World": case "world":
                textChunks.add(customize(spaced, "blue"));
                break;
            case "Stack Overflow":
                textChunks.add(customize(spaced, "orange", "Arial Bold", 15));
            default:
                textChunks.add(customize(spaced, "black", "Arial",  13));
                break;
        }
    }

    bundle.getChildren().addAll(textChunks);
    return bundle;
}

public Text customize(String word, String color) {
    return TextBuilder.create().text(word).fill(Paint.valueOf(color)).build();
}

public Text customize(String word, String color, String font) {
    return TextBuilder.create()
            .text(word)
            .fill(Paint.valueOf(color))
            .font(Font.font(font, 12)).build();
}

public Text customize(String word, String color, String font, int fontSize) {
    return TextBuilder.create()
            .text(word)
            .fill(Paint.valueOf(color))
            .font(Font.font(font, fontSize)).build();
}

}

"Working Example"

解决方案

Create a custom cellfactory for your ListView and have it generate cells containing a FlowPane with different Text instances, each with different styles. I created a sample to demonstrate this method.

Sample output:

In Java 8 you can you can use the TextFlow to style your text rather than a combination of different Text instances in a FlowPane.

这篇关于Javafx文本多字彩色化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆