如何嵌入.ttf字体是JavaFx 2.2? [英] How to embed .ttf fonts is JavaFx 2.2?

查看:364
本文介绍了如何嵌入.ttf字体是JavaFx 2.2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  #text {
-fx-font-family: URL(资源/ fontName.ttf);

$ / code>

我也尝试在url中添加引号,即 url(resources / fontName.ttf); ,但不起作用。我也设置组件的CSS ID,所以不能是问题。有没有其他工作方式可以这样做?我已经看过。

CustomFontApp.java

  import javafx.application.Application; 
import javafx.geometry.Pos;
导入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;

//演示使用自定义字体。
public class CustomFontApp extends Application {
public static void main(String [] args){launch(args); }
@Override public void start(Stage stage){
stage.setTitle(TRON Synopsis);

//载入tron字体。
Font.loadFont(
CustomFontApp.class.getResource(TRON.TTF)。toExternalForm(),
10
);

标签title =新标签(TRON);
title.getStyleClass()。add(title);

标签标题=新标签(在另一个现实中设置的科幻轻弹);
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

),
字幕
);

//布置场景。
最终场景场景=新场景(布局);
scene.getStylesheets()。add(getClass()。getResource(custom-font-styles.css)。toExternalForm());
stage.setScene(scene);
stage.show();




custom-font-styles.css

  / **文件:custom-font-styles.css 
*与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,并不会建议,如果你可以。而是加载Java代码中的字体(正如我在答案中所做的那样),然后加载需要字体的FXML或样式表。


Firstly, I am quite a new guy in coding. I need to embed a font in my javaFXML-based app and don't know how to do it. I have pasted the font, fontName.ttf in a "resources" folder in the root of the sources of my project, ie App/src/app/resources. I have set the CSS for the component (text) as

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

I have also tried adding inverted commas in the url, ie url("resources/fontName.ttf");, but it doesn't work. I have also set the css id for the component, so that can't be the problem. Is there any other working way to do so? I have seen http://fxexperience.com/2010/05/how-to-embed-fonts/, but it doesn't work since I have jdk 1.7 u21. Any ideas for a correct way to embed fonts?

解决方案

Solution Approach

I updated the sample from Javafx How to display custom font in webview? to demonstrate using a custom true-type font in JavaFX controls styled using CSS.

Key points are:

  1. Place the font in the same location as your application class and ensure your build system places it in your binary build package (e.g. application jar file).
  2. Load the code font in your JavaFX code before you apply a style which uses it. Font.loadFont(CustomFontApp.class.getResource("TRON.TTF").toExternalForm(), 10);
  3. To use the custom font in a style class use the -fx-font-family css attribute and just reference the name of the font (e.g. in this case "TRON").
  4. Create and load a stylesheet which defines the style classes.
  5. Apply style classes to your controls.

Additional Information

If you are using Java 8, you may be interested in Use web(Google) fonts in JavaFX.

Sample Output Using a Custom Font

Sample Code

The example relies on a TRON.TTF font which you can download from 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();
  }
}

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

On FXML Usage

Font.loadFont(url, size) is a static method taking two parameters. I don't think you can invoke font.loadFont from FXML and wouldn't advise it if you could. Instead, load the font in Java code (as I have done in my answer) before you load your FXML or style sheet which requires the font.

这篇关于如何嵌入.ttf字体是JavaFx 2.2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆