首先,我在编程方面是个新手。我需要在基于java FXML的应用程序中嵌入一个字体,但我不知道该怎么做。我已经将字体fontName.ttf
粘贴到我的项目源代码的根目录下的“资源”文件夹中,即App/src/app/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/,但由于我使用的是JDK1.7 u21,所以它无法工作。对嵌入字体的正确方法有什么想法吗?
发布于 2013-05-31 22:32:40
解决方案方法
我更新了Javafx How to display custom font in webview?中的示例,以演示如何在使用CSS样式的JavaFX控件中使用自定义的true-type字体。
关键点是:
Font.loadFont(CustomFontApp.class.getResource("TRON.TTF").toExternalForm(), 10);
"TRON"
).附加信息
如果您使用的是Java8,那么您可能对Use web(Google) fonts in JavaFX感兴趣。
字体集合
如果您的字体文件是.ttc
格式,并且在单个文件中包含多个字体,则使用Font.loadFonts
接口(而不是Font.loadFont
)。请注意,Font.loadFonts
仅在JDK9之后才可用,在早期版本中不可用。
使用自定义字体的示例输出
示例代码
该示例依赖于您可以从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代码中加载字体(正如我在回答中所做的那样)。
发布于 2014-09-14 11:45:30
我知道您没有要求在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
https://stackoverflow.com/questions/16855677
复制