the title already describes my problem. I want to color single letters or sequences of letters in a JFX Button but not the whole text. I found a solution for swing components Is it possible to change the text color in a string to multiple colors in Java? but not for javafx yet. Can anyone help me plz?
问问题
1422 次
2 回答
5
看一下这个
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);
}
}
输出
于 2012-10-16T14:14:42.223 回答
1
这只是 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 岁的问题。;-)
于 2019-10-11T07:41:43.193 回答