,即一旦該標準的標籤文本只能有一個單一的風格對於給定的標籤。
但是,您可以使用TextFlow輕鬆地混合文本樣式。通常你可以直接引用TextFlow而不把它放在一個封閉的Label中。
如果您希望將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);
}
}
三江源非常多,花了幾個小時試圖做這一件簡單的事 – RhysBuddy