【问题标题】:How to embed .ttf fonts in JavaFx 2.2?如何在 JavaFx 2.2 中嵌入 .ttf 字体?
【发布时间】:2013-05-27 04:19:21
【问题描述】:

首先,我是一个编码方面的新手。我需要在我的基于 java FXML 的应用程序中嵌入字体,但不知道该怎么做。我已将字体fontName.ttf 粘贴到项目源代码根目录下的“resources”文件夹中,即App/src/app/resources。我已将组件(文本)的 CSS 设置为

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

我也尝试在网址中添加引号,即url("resources/fontName.ttf");,但它不起作用。我还为组件设置了 CSS id,所以这不是问题。还有其他工作方式吗?我见过http://fxexperience.com/2010/05/how-to-embed-fonts/,但它不起作用,因为我有JDK 1.7 u21。关于嵌入字体的正确方法有什么想法吗?

【问题讨论】:

    标签: fonts embed javafx fxml


    【解决方案1】:

    解决方法

    我从 Javafx How to display custom font in webview? 更新了示例,以演示在使用 CSS 样式的 JavaFX 控件中使用自定义 true-type 字体。

    重点是:

    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 感兴趣。

    字体集

    如果您的字体文件是.ttc 格式,并且在一个文件中包含多个字体,则使用Font.loadFonts API(而不是Font.loadFont)。请注意,Font.loadFonts 仅在 JDK 9 之后可用,在早期版本中不可用。

    使用自定义字体的示例输出

    示例代码

    该示例依赖于您可以从dafont 下载的 TRON.TTF 字体。

    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();
      }
    }
    

    custom-font-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 调用 font.loadFont 并且如果可以的话也不会建议您这样做。相反,在加载需要字体的 FXML 或样式表之前,在 Java 代码中加载字体(正如我在回答中所做的那样)。

    【讨论】:

    • 出现错误并说找不到符号:loadFont in class Font。需要三个标识符。 (我将 fxml 中的字体导入为import javafx.scene.text.Font;
    • 在答案中添加了关于 FXML 使用的部分。
    • 出现错误。在这里查看:docs.google.com/document/d/…
    • 我无法重现您的错误 - 我的答案中的解决方案对我有用。如果您希望获得进一步的帮助,您需要发布 sscce 以复制您的错误。
    • 猜测错误与其他代码逻辑有关。对不起,如果我浪费了你的时间,但感谢你告诉答案
    【解决方案2】:

    我知道您没有要求以纯程序化方式在 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

    【讨论】:

    • 写这篇文章的时候已经快 2022 年(2021 年 12 月 14 日)了,在我看来,一些用于在 JavaFX 中加载字体的方法不再有效。这对我有用,所以谢谢@Chris(+1up ?)嘿,这是我,马里奥!
    猜你喜欢
    • 2019-03-15
    • 2015-07-16
    • 1970-01-01
    • 2013-05-25
    • 2021-02-04
    • 2012-11-09
    • 1970-01-01
    • 2022-10-02
    相关资源
    最近更新 更多