2015-10-14 145 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