如何embedded.ttf字体是JavaFx 2.2?

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

 #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。 任何想法正确的方式来embedded字体?

解决scheme

我更新了从JavaFX的示例如何在webview中显示自定义字体? 演示如何在使用CSS的JavaFX控件中使用自定义的真实types字体。

要点是:

  1. 将字体放在与您的应用程序类相同的位置,并确保您的构build系统将其放置在二进制构build包(例如应用程序jar文件)中。
  2. 在应用使用它的样式之前,将代码字体加载到JavaFX代码中。 Font.loadFont(CustomFontApp.class.getResource("TRON.TTF").toExternalForm(), 10);
  3. 要在样式类中使用自定义字体,请使用-fx-font-family css属性并仅引用字体的名称(例如在本例中为"TRON" )。
  4. 创build并加载定义样式类的样式表。
  5. 将样式类应用于您的控件。

附加信息

如果您使用的是Java 8,则可能对使用JavaFX中的Web(Google)字体感兴趣。

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

tronfonts

示例代码

该示例依赖于可以从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.comhttp://img.dovov.comM/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调用font.loadFont,并不会build议,如果你可以。 相反,加载您的FXML或需要字体的样式表之前,在Java代码中加载字体(正如我在我的答案中所做的那样)。

我知道你没有要求在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(); } } 

为我做了这份工作。 你可以把字体放在任何你想要的地方,只要确定你适应的path。

您可以在这里find更多关于在java fx应用程序中使用字体的信息。

HTH