Pra*_*and 22 embed fonts javafx fxml
首先,我是编码领域的新人.我需要在我的基于javaFXML的应用程序中嵌入一个字体,不知道该怎么做.我已经将字体粘贴在fontName.ttf项目源根目录中的"resources"文件夹中,即App/src/app/resources.我已将组件(文本)的CSS设置为
#text {
-fx-font-family: url(resources/fontName.ttf);
}
Run Code Online (Sandbox Code Playgroud)
我也尝试在url中添加引号,即url("resources/fontName.ttf");,但它不起作用.我还为组件设置了css id,这样就不会出现问题了.还有其他工作方式吗?我见过http://fxexperience.com/2010/05/how-to-embed-fonts/,但由于我有jdk 1.7 u21,它不起作用.有关正确嵌入字体的方法的想法吗?
jew*_*sea 42
解决方法
我从Javafx更新了示例如何在webview中显示自定义字体?演示在使用CSS设置样式的JavaFX控件中使用自定义真实字体.
要点是:
Font.loadFont(CustomFontApp.class.getResource("TRON.TTF").toExternalForm(), 10);-fx-font-familycss属性并仅引用字体名称(例如,在本例中"TRON").附加信息
如果您使用的是Java 8,您可能会对JavaFX中的使用Web(Google)字体感兴趣.
使用自定义字体输出样本

示例代码
该示例依赖于TRON.TTF字体,您可以从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();
}
}
Run Code Online (Sandbox Code Playgroud)
自定义字体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;
}
Run Code Online (Sandbox Code Playgroud)
关于FXML用法
Font.loadFont(url,size)是一个带有两个参数的静态方法.我不认为你可以从FXML调用font.loadFont,如果可以,也不会建议它.相反,在加载需要字体的FXML或样式表之前,在Java代码中加载字体(正如我在答案中所做的那样).
Chr*_*ris 16
我知道你没有要求在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();
}
}
Run Code Online (Sandbox Code Playgroud)
这对我来说很重要.您可以将字体放在任何位置,只需确保调整路径即可.
您可以在此处找到有关在java fx应用程序中使用字体的更多信息.
HTH
| 归档时间: |
|
| 查看次数: |
32287 次 |
| 最近记录: |