【问题标题】:Multicolored labels or other text areas in JavaFXJavaFX 中的多色标签或其他文本区域
【发布时间】:2020-02-03 15:22:00
【问题描述】:

我使用 JavaFX for GUI 编写我的应用程序。我需要将列表视图放在界面中,而应用程序将获得彩色消息(来自其他 API,我已经有转换器)。我需要在listview中放入什么,什么可以支持多色文本?我需要在一个列表条目中添加多种颜色。

我已经尝试使用 TextFlow,但没有任何改变...文本是白色的。

// In converter (list of Messages to TextFlow)
public static TextFlow getTextFlow(List<Message> list) {
        TextFlow flow = new TextFlow();
        for(Message msg : list) {
            Text text = new Text(msg.getFullText());
            MessageStyle style = msg.getStyle();

            ChatColor color = style.getColor();
            String textstyle = "";
            if(!(color.equals(ChatColor.NONE) || color.equals(ChatColor.NONE)))
                textstyle += "-fx-text-fill: #" + TextColor.valueOf(color.name()).getHex() + ";";

            System.out.println(textstyle);
            for(ChatFormat f : style.getFormats()) {
                System.out.println(f.toString());
                switch (f) {
                case BOLD:
                    if(!textstyle.equals("-fx-font-weight: bold;")) textstyle += " ";
                    textstyle += "";
                    break;
                case ITALIC:
                    if(!textstyle.equals("-fx-font-style: italic;")) textstyle += " ";
                    textstyle += "";
                    break;
                case STRIKETHROUGH:
                    text.setStrikethrough(true);
                    break;
                case UNDERLINED:
                    text.setUnderline(true);
                    break;
                default:
                    break;
                }
            }

            text.setStyle(textstyle);
            flow.getChildren().add(text);
        }

        System.out.println(flow.getChildren());
        return flow;
    }
// In message add method
public void handle() {
        if(packet.getType().equals(MessageType.NOTIFICATION)) return;

        Message msg = packet.getMessage();
        System.out.println(msg.toString());

        TextFlow flow = MessageUtils.getTextFlow(msg.getExtra());
        Platform.runLater(() -> {
            MainController.getInstance().getLogsListView().getItems().add(flow);
        });

    }

【问题讨论】:

  • 我没有看到ListView setCellFactory
  • @Sedrick,不,我知道单元格,但我需要这个单元格中的彩色文本...
  • 实现目标所需的步骤与链接中的相同。您需要使用setCellFactory 和一些条件来确定什么文本得到什么颜色。
  • TextFlow 与多个Text 节点一起使用是彩色文本的正确方法。如果这对您不起作用,请创建 minimal reproducible example 演示问题并通过edit 将其添加到您的问题中。

标签: javafx


【解决方案1】:

我尝试在 TextFlow 中将文本替换为标签,它可以工作! 感谢您的回答。 请参阅下面的转换器代码。上面的其他代码我没有编辑。

        TextFlow flow = new TextFlow();
        for(Message msg : list) {
            Label text = new Label(msg.getFullText());
            MessageStyle style = msg.getStyle();

            ChatColor color = style.getColor();
            String textstyle = "";
            if(!(color.equals(ChatColor.NONE) || color.equals(ChatColor.NONE)))
                textstyle += "-fx-text-fill: #" + TextColor.valueOf(color.name()).getHex() + ";";

            System.out.println(textstyle);
            for(ChatFormat f : style.getFormats()) {
                System.out.println(f.toString());
                switch (f) {
                case BOLD:
                    if(!textstyle.equals("")) textstyle += " ";
                    textstyle += "-fx-font-weight: bold;";
                    break;
                case ITALIC:
                    if(!textstyle.equals("")) textstyle += " ";
                    textstyle += "-fx-font-style: italic;";
                    break;
                case STRIKETHROUGH:
                    if(!textstyle.equals("")) textstyle += " ";
                    textstyle += "-fx-strikethrough: true;";
                    break;
                case UNDERLINED:
                    text.setUnderline(true);
                    break;
                default:
                    break;
                }
            }

            text.setStyle(textstyle);
            flow.getChildren().add(text);
        }

        return flow;
    }

【讨论】:

  • 啊,我现在知道问题出在哪里了。您正在使用-fx-text-fill,但这不是Text 的CSS 属性。使用 Text 时,您必须使用 -fx-fill 通过 CSS 更改文本颜色。欲了解更多信息,请查看JavaFX CSS Reference Guide
猜你喜欢
  • 2016-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多