gpt4 book ai didi

Java 文本字段 2 颜色

转载 作者:行者123 更新时间:2023-12-02 02:44:51 25 4
gpt4 key购买 nike

有人知道如何在 Java 文本字段 (Java FX)(在标签上)的一个句子上放置两种不同的颜色吗?我也使用 CSS,但更喜欢直接在类中设置它。

最佳答案

您需要使用TextFlow

Rich Text and Bidirectional Support

You can create several Text nodes and lay them out in a single text flow by using the TextFlow layout pane. The TextFlow object employs the text and font of each Text node but ignores the wrapping width and the x and y properties of its children. The TextFlow object uses its own width and text alignment to determine the location of each child. Example 39-12 shows three Text nodes that have different fonts and text laid out in a TextFlow pane.

String family = "Helvetica";
double size = 50;

TextFlow textFlow = new TextFlow();
textFlow.setLayoutX(40);
textFlow.setLayoutY(40);

// Red
Text text1 = new Text("Hello ");
text1.setFont(Font.font(family, size));
text1.setFill(Color.RED);

// Orange
Text text2 = new Text("Bold");
text2.setFill(Color.ORANGE);
text2.setFont(Font.font(family, FontWeight.BOLD, size));

// Green
Text text3 = new Text(" World");
text3.setFill(Color.GREEN);
text3.setFont(Font.font(family, FontPosture.ITALIC, size));

textFlow.getChildren().addAll(text1, text2, text3);

Group group = new Group(textFlow);
Scene scene = new Scene(group, 500, 150, Color.WHITE);
stage.setTitle("Hello Rich Text");
stage.setScene(scene);
stage.show();

https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/text-settings.htm

上面的示例将生成带有红色、橙色和绿色(并且具有不同样式)的Hello Bold World。除非您想要严格的 CSS 解决方案,否则无法使用 TextField 完成您想要的操作。 TextFlow 是必经之路

关于Java 文本字段 2 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44768514/

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