2013-05-31 48 views
16

首先,我在編碼方面相當新穎。我需要在基於javaFXML的應用程序中嵌入字體,但不知道如何去做。我已將字體fontName.ttf粘貼到我的項目源的根目錄(即App/src/app/resources)的「resources」文件夾中。我已經爲組件(文本)的CSS作爲如何嵌入.ttf字體是JavaFx 2.2?

#text { 
    -fx-font-family: url(resources/fontName.ttf); 
} 

我也試圖在URL中添加引號,即url("resources/fontName.ttf");,但它不工作。我也爲組件設置了css id,所以這不成問題。還有其他工作方式嗎?我看過http://fxexperience.com/2010/05/how-to-embed-fonts/,但它不起作用,因爲我有jdk 1.7 u21。任何正確的方式來嵌入字體的想法?

回答

31

解決方案方法

我從Javafx How to display custom font in webview?更新所述樣品使用用CSS樣式JavaFX的控制自定義真實型字體來證明。

關鍵點是:

  1. 放置在相同的位置,你的應用程序類的字體,並確保在您的二元構建軟件包(如應用程序JAR文件)你的編譯系統的地方吧。
  2. 在應用使用它的樣式之前,在您的JavaFX代碼中加載代碼字體。 Font.loadFont(CustomFontApp.class.getResource("TRON.TTF").toExternalForm(), 10);
  3. 要在樣式類中使用自定義字體,請使用-fx-font-family css屬性並只引用字體的名稱(例如在此例中爲"TRON")。
  4. 創建並加載定義樣式類的樣式表。
  5. 將樣式類應用於您的控件。

其他信息

如果您使用的是Java 8中,您可能感興趣的Use web(Google) fonts in JavaFX

樣本輸出使用自定義字體

tronfonts

示例代碼

的例子依賴於TRON.TTF字體,你可以從dafont下載。

CustomFontApp.java

import javafx.application.Application; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.image.*; 
import javafx.scene.layout.VBox; 
import javafx.scene.text.*; 
import javafx.stage.Stage; 

// demonstrates the use of a custom font. 
public class CustomFontApp extends Application { 
    public static void main(String[] args) { launch(args); } 
    @Override public void start(Stage stage) { 
    stage.setTitle("TRON Synopsis"); 

    // load the tron font. 
    Font.loadFont(
     CustomFontApp.class.getResource("TRON.TTF").toExternalForm(), 
     10 
    ); 

    Label title = new Label("TRON"); 
    title.getStyleClass().add("title"); 

    Label caption = new Label("A sci-fi flick set in an alternate reality."); 
    caption.getStyleClass().add("caption"); 
    caption.setMaxWidth(220); 
    caption.setWrapText(true); 
    caption.setTextAlignment(TextAlignment.CENTER); 

    VBox layout = new VBox(10); 
    layout.setStyle("-fx-padding: 20px; -fx-background-color: silver"); 
    layout.setAlignment(Pos.CENTER); 
    layout.getChildren().setAll(
     title, 
     new ImageView(
     new Image(
      "http://ia.media-imdb.com/images/M/MV5BMTY5NjM2MjAwOV5BMl5BanBnXkFtZTYwMTgyMzA5.V1.SY317.jpg" 
     ) 
    ), 
     caption 
    ); 

    // layout the scene. 
    final Scene scene = new Scene(layout); 
    scene.getStylesheets().add(getClass().getResource("custom-font-styles.css").toExternalForm()); 
    stage.setScene(scene); 
    stage.show(); 
    } 
} 

定製字體styles.css的

/** file: custom-font-styles.css 
* Place in same directory as CustomFontApp.java 
*/ 

.title { 
    -fx-font-family: "TRON"; 
    -fx-font-size: 20; 
} 

.caption { 
    -fx-font-family: "TRON"; 
    -fx-font-size: 10; 
} 

在FXML用法

Font.loadFont(url, size)是一個靜態方法以兩個參數。我不認爲你可以調用字體。來自FXML的loadFont,如果可以的話,不會提供建議。相反,在加載需要字體的FXML或樣式表之前,請使用Java代碼加載字體(正如我在答案中所做的那樣)。

+0

錯誤出現,說找不到符號:'loadFont'類'Font'。預計有三個標識符。 (我在fxml中導入字體爲'import javafx.scene.text.Font;') –

+1

在答案中添加了關於FXML用法的章節。 – jewelsea

+0

出現錯誤。在此處查看:https://docs.google.com/document/d/1GSQeo8CHQhMEU_nS-gl6yWL1qQF4T08kYwPa_OELer4/edit?usp=sharing –

10

我知道你沒有要求在Java FX應用程序中使用自定義的TTF字庫純粹的編程方式,但我想也許這可以幫助別人看到的綱領性版本:

public class Test2 extends Application { 

    public static void main(String[] args) { 
    launch(args); 
    } 

    public void start(final Stage primaryStage) { 
    Group rootGroup = new Group(); 

    // create a label to show some text 
    Label label = new Label("Demo Text"); 
    try { 
     // load a custom font from a specific location (change path!) 
     // 12 is the size to use 
     final Font f = Font.loadFont(new FileInputStream(new File("./myFonts/TRON.TTF")), 12); 
     label.setFont(f); // use this font with our label 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    rootGroup.getChildren().add(label); 

    // create scene, add root group and show stage 
    Scene scene = new Scene(rootGroup, 640, 480, Color.WHITE); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
    } 
} 

該做的爲我工作。你可以將字體放在任何你想要的地方,只要確保你適應了路徑。

你可以找到許多關於using fonts inside java fx apps here.

HTH