【问题标题】:Make Specific word italic in javafx label在 javafx 标签中使特定单词斜体
【发布时间】:2016-01-13 05:15:29
【问题描述】:

我想在我的标签斜体中使用一个特定的词,但我找不到任何解决方案,我到处寻找并尝试了很多不同的方法。

 Label reference = new Label(lastNameText + ", " + firstNameText + ". (" + yearText + "). " 
                    + titleOfArticleText + ". " + titleOfJournalText + ", " 
                    + volumeText + ", " + pageNumbersText + ". " + doiText);

背景信息 - 我希望“titleOfJournalText”是斜体,其余的只是简单的,顺便说一句,它们都是字符串,它们曾经在他们自己的文本字段中

【问题讨论】:

    标签: java user-interface javafx label italic


    【解决方案1】:

    对于给定的标签,标准标签文本只能有一个样式。

    不过,您可以使用TextFlow 轻松混合文本样式。通常您可以直接引用 TextFlow 而无需将其放在封闭的标签中。

    如果您愿意,您仍然可以将 TextFlow 放置在标签中,方法是将 TextFlow 设置为标签的图形。请注意,当您这样做时,标签的内置省略功能(如果没有足够的空间来显示标签,标签文本将被截断为点)将无法与 TextFlow 一起使用。

    这是一个引用爱因斯坦狭义相对论的小示例程序。

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.text.*;
    import javafx.stage.Stage;
    
    public class StyledLabel extends Application {
    
        public static final Font ITALIC_FONT =
                Font.font(
                        "Serif",
                        FontPosture.ITALIC,
                        Font.getDefault().getSize()
                );
    
        @Override
        public void start(final Stage stage) throws Exception {
            Text lastNameText = new Text("Einstein");
            Text firstNameText = new Text("Albert");
            Text yearText = new Text("1905");
            Text titleOfArticleText = new Text("Zur Elektrodynamik bewegter Körper");
            Text titleOfJournalText = new Text("Annalen der Physik");
            titleOfJournalText.setFont(ITALIC_FONT);
            Text volumeText = new Text("17");
            Text pageNumbersText = new Text("891-921");
            Text doiText = new Text("10.1002/andp.19053221004");
    
            Label reference = new Label(
                    null,
                    new TextFlow(
                            lastNameText, new Text(", "),
                            firstNameText, new Text(". ("),
                            yearText, new Text("). "),
                            titleOfArticleText, new Text(". "),
                            titleOfJournalText, new Text(", "),
                            volumeText, new Text(", "),
                            pageNumbersText, new Text(". "),
                            doiText
                    )
            );
    
            stage.setScene(new Scene(reference));
            stage.show();
        }
    
        public static void main(String[] args) throws Exception {
            launch(args);
        }
    }
    

    【讨论】:

    • 非常感谢,花了几个小时尝试做这件简单的事情
    猜你喜欢
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多