【问题标题】:Color Single Letters in JavaFX ButtontextsJavaFX Buttontexts 中的彩色单个字母
【发布时间】:2012-10-06 13:53:47
【问题描述】:

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

【问题讨论】:

    标签: button javafx-2 letters text-coloring


    【解决方案1】:

    看看这个

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

    输出

    【讨论】:

    • 哇,非常感谢!这正是我想要的。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 2021-12-06
      • 1970-01-01
      • 2017-08-19
      • 2014-08-21
      • 1970-01-01
      相关资源
      最近更新 更多