Java 文本字段 2 颜色

标签 java javafx colors textfield

有人知道如何在 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/

相关文章:

java - Android:使用notifyDataSetChanged更新listview

java - 来自应用程序引擎的分析 API AppIdentityCredential 权限不足

java - Viewholder onClickListener 在 android 中引用了错误的 View

JavaFX 使用 Gluon SceneBuilder InvocableTargetException

java - 如何检测用户键盘是否在 Java 中的 AZERTY 中?

javafx - 按特定列对 TableView 进行排序 Javafx

android - 如何更改我的 Android 应用程序中所有文本的默认颜色?

java - 在 GUI 应用程序中构建“关于我”页面

java - 用圆圈填充 Jbutton

c# - 获取两种颜色 HEX refs 或 RGB 之间的颜色