2015-10-14 149 views
1

我想在标签斜体中做一个特定的单词,但无法找到任何解决方案,ive无处不在,尝试了很多不同的方法。在javafx标签中制作特定的斜体字

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

背景信息 - 我想“titleOfJournalText”是斜体,其余的只是普通的,都是字符串BTW这在他们自己的文本框

回答

0

,即一旦该标准的标签文本只能有一个单一的风格对于给定的标签。

但是,您可以使用TextFlow轻松地混合文本样式。通常你可以直接引用TextFlow而不把它放在一个封闭的Label中。

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

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

reference

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

三江源非常多,花了几个小时试图做这一件简单的事 – RhysBuddy