javafx - ControlsFX 通知中的粗体文本部分

标签 javafx controlsfx

我正在尝试在通知正文中写入粗体文本。

Notifications.create()
    .title("My Title")
    .text("This is <bold>text</bold>")
    .showInformation();

据我所知,只有 .text("my text") 方法可以设置通知的文本。 但是我想在那里使用 TextFlow 来修改文本的某些部分。

我也曾尝试为此使用 CSS,但这只能让我更改通知的整体外观,因为明确不支持通过 CSS 设置特定文本。

我现在的问题是:有没有办法对通知中的部分文本设置不同的样式?

最佳答案

现在可以使用了。 解决方案是使用.graphic(Node node)。如果您仍想在左侧显示图像,则需要将所有内容都放在 Pane 中并添加一些填充。 我从 here 中提取了原始图像.

        BorderPane borderPane = new BorderPane();
    borderPane.setLeft(new ImageView(Controller.class.getResource("/dialog-information.png").toExternalForm()));
    TextFlow textFlow = new TextFlow();
    textFlow.setPadding(new Insets(15, 0, 5, 5));
    Text text1 = new Text("This is ");
    Text text2 = new Text("bold");
    text2.setStyle("-fx-font-weight: bold");
    textFlow.getChildren().addAll(text1, text2);
    borderPane.setRight(textFlow);
    Notifications.create()
            .title("My Title")
            .graphic(borderPane)
            .hideAfter(Duration.seconds(10))
            .show();

使用 .show() 而不是 .showInformation/Error/Warning() 很重要,因为那样会覆盖 BorderPane。

结果: example of Notification with TextFlow

关于javafx - ControlsFX 通知中的粗体文本部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70054464/

相关文章:

java - 如何在 JavaFX 中将 SimpleLocalizedStringProperty (controlsfx-plugin) 与 ResourceBundle 结合使用

css - 如何覆盖 javafx 中的默认 -fx-focus-color css 属性?

java - 仅在使用 ValidationSupport 提交后验证 JavaFX 表单

Javafx controlfx 调用 show() 时通知不会立即显示

JavaFX TableView 列宽更改事件监听器

java - ControlsFX RangeSlider 无法禁用

java - 自定义检查组合框

带有监听器的 JavaFX TextArea 给出 : “java.lang.IllegalArgumentException: The start must be <= the end”

javafx - 每个用户或每台机器安装的 Inno Setup 自定义对话框

java - JavaFX中标签的自动换行