JavaFX Buttontexts中的单色字母颜色

JavaFX Buttontexts中的单色字母颜色,button,javafx-2,letters,text-coloring,Button,Javafx 2,Letters,Text Coloring,标题已经描述了我的问题。我想给JFX按钮中的单个字母或字母序列上色,但不想给整个文本上色。我找到了swing组件的解决方案,但还没有找到javafx。有人能帮我吗?请看这个 public class ColoredButtonTextDemo extends Application { @Override public void start(Stage primaryStage) { Button btn = new Button(); btn.

标题已经描述了我的问题。我想给JFX按钮中的单个字母或字母序列上色,但不想给整个文本上色。我找到了swing组件的解决方案,但还没有找到javafx。有人能帮我吗?请看这个

public class ColoredButtonTextDemo extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);

        HBox coloredTextBox = HBoxBuilder.create().spacing(0).children(
                LabelBuilder.create().text("Say ").textFill(Color.YELLOW).build(),
                LabelBuilder.create().text("'").textFill(Color.DARKBLUE).build(),
                LabelBuilder.create().text("Hell").textFill(Color.RED).build(),
                LabelBuilder.create().text("o ").textFill(Color.GREEN).build(),
                LabelBuilder.create().text("W").textFill(Color.BLUE).build(),
                LabelBuilder.create().text("orld!").textFill(Color.DARKMAGENTA).build(),
                LabelBuilder.create().text("'").textFill(Color.DARKBLUE).build()//
                ).build();

        btn.setGraphic(coloredTextBox);
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        Scene scene = new Scene(root, 300, 250);
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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


这只是Uluk Biy答案的FXML形式:

<Button>
    <graphic>
        <HBox spacing="0.0">
            <Label text="Colors " style="-fx-text-fill: yellow; -fx-background-color: #1156cf;" />
            <Label text="are " style="-fx-text-fill: red; -fx-background-color: #11cf56;" />
            <Label text="cool!" style="-fx-text-fill: blue; -fx-background-color: #cf1156;" />
        </HBox>
    </graphic>
</Button>


…是的,我喜欢回答7年前的问题

哇,太谢谢你了!这正是我想要的。