首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何在JavaFx 2.2中嵌入.ttf字体?

如何在JavaFx 2.2中嵌入.ttf字体?
EN

Stack Overflow用户
提问于 2013-05-31 18:57:26
回答 2查看 39.8K关注 0票数 25

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

代码语言:javascript
代码运行次数:0
运行
复制
#text {
    -fx-font-family: url(resources/fontName.ttf);
}

我也试过在url中添加反逗号,比如url("resources/fontName.ttf");,但不起作用。我还设置了组件的CSS id,所以这不是问题所在。有没有其他可行的方法呢?我见过http://fxexperience.com/2010/05/how-to-embed-fonts/,但由于我使用的是JDK1.7 u21,所以它无法工作。对嵌入字体的正确方法有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-01 06:32:40

解决方案方法

我更新了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. To在样式类中使用自定义字体,使用style css属性并仅引用字体的名称(例如,在本例中为"TRON").
  4. Create,然后将定义样式classes.
  5. Apply样式类的样式表加载到控件中。

附加信息

如果您使用的是Java8,那么您可能对Use web(Google) fonts in JavaFX感兴趣。

字体集合

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

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

示例代码

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

CustomFontApp.java

代码语言:javascript
代码运行次数:0
运行
复制
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

代码语言:javascript
代码运行次数:0
运行
复制
/** 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代码中加载字体(正如我在回答中所做的那样)。

票数 46
EN

Stack Overflow用户

发布于 2014-09-14 19:45:30

我知道您没有要求在java fx应用程序中使用自定义TTF字体的纯编程方式,但我认为它可能会帮助某些人看到编程版本:

代码语言:javascript
代码运行次数:0
运行
复制
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

票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16855677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档