JavaFX Buttontexts中的单色字母

时间:2012-10-16 12:51:01

标签: button javafx-2 letters text-coloring

标题已经描述了我的问题。我想在JFX按钮中为单个字母或字母序列着色,而不是整个文本。我找到了摆动组件Is it possible to change the text color in a string to multiple colors in Java?的解决方案,但还没有找到javafx的解决方案。任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:3)

检查出来

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);
    }
}

输出

enter image description here

答案 1 :(得分:0)

这只是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岁的问题。 ;-)