无法使用 CSS 样式在 javaFX 中使用自定义字体
Cannot use custom font in javaFX using CSS Styling
我正在尝试使用 CSS 将全局字体添加到我的 javaFX 应用程序。
我按照 this answer,但是我似乎无法加载字体,错误消息也没有帮助。
jan 12, 2022 12:24:25 PM javafx.css.CssParser reportException
WARNING: Please report java.lang.NullPointerException at:
错误消息到此结束,没有显示问题出在哪里。
我的代码目录:
pacman directory
我的main.java:
package finalPacman;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader(getClass().getResource("pacman.fxml"));
Parent root = loader.load();
primaryStage.setTitle("PacMan");
Controller controller = loader.getController();
root.setOnKeyPressed(controller);
double sceneWidth = controller.getBoardWidth() + 20.0;
double sceneHeight = controller.getBoardHeight() + 100.0;
Scene scene = new Scene(root, sceneWidth, sceneHeight);
scene.getStylesheets().clear();
scene.getStylesheets().add(getClass().getResource("pacman.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
root.requestFocus();
}
public static void main(String[] args) {
launch(args);
}
}
我的pacman.css:
@font-face {
font-family: 'Pixeboy';
src: url("/src/res/fonts/Pixeboy.ttf");
}
.root{
-fx-font-size: 16;
-fx-font-family: "Pixeboy";
}
@font-face
可以工作但是:
它需要在您的 CSS 文件中排在第一位。
它不能在 src 中使用前导斜线 URL.
src URL 必须定位
- 网络字体通过有效的完整 URL,例如
http:
、https:
或 file:
协议或
- 要将其作为资源获取,它必须是包含@font-face 指令的CSS 文件的相对 路径。
如果一切设置正确,则以下答案中的说明可以正常工作(使用 JavaFX 17 验证):
- JavaFX custom Fonts
适用于问题中定义的资源的示例 URL 是相对于 CSS 文件的 src URL:
src: url("fonts/Pixeboy.ttf");
假设字体实际上位于引用 @font-face
.
的 CSS 文件的相对位置
当我测试时,我使用了上面答案中的 CSS and fonts 链接。
疑难解答
如果您在 src URL 中为 @font-face 使用前导斜杠(例如 src: url("/fonts/Pixeboy.ttf");
),您将收到以下消息并且字体不会正在加载:
Jan 12, 2022 2:31:09 PM javafx.css.CssParser reportException
WARNING: Please report java.lang.NullPointerException at:
即使从 class/module 路径的根目录找到字体文件的路径正确,也会生成错误消息并且不会加载字体。
如果您为不存在的字体指定位置(例如 src: url("wrongfoldername/Pixeboy.ttf");
,您将收到以下错误消息并且不会加载字体:
Jan 12, 2022 2:43:18 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
INFO: Could not load @font-face font [file:/<build-output-dir>/wrongfoldername/Pixeboy.ttf]
我正在尝试使用 CSS 将全局字体添加到我的 javaFX 应用程序。 我按照 this answer,但是我似乎无法加载字体,错误消息也没有帮助。
jan 12, 2022 12:24:25 PM javafx.css.CssParser reportException
WARNING: Please report java.lang.NullPointerException at:
错误消息到此结束,没有显示问题出在哪里。
我的代码目录: pacman directory
我的main.java:
package finalPacman;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader(getClass().getResource("pacman.fxml"));
Parent root = loader.load();
primaryStage.setTitle("PacMan");
Controller controller = loader.getController();
root.setOnKeyPressed(controller);
double sceneWidth = controller.getBoardWidth() + 20.0;
double sceneHeight = controller.getBoardHeight() + 100.0;
Scene scene = new Scene(root, sceneWidth, sceneHeight);
scene.getStylesheets().clear();
scene.getStylesheets().add(getClass().getResource("pacman.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
root.requestFocus();
}
public static void main(String[] args) {
launch(args);
}
}
我的pacman.css:
@font-face {
font-family: 'Pixeboy';
src: url("/src/res/fonts/Pixeboy.ttf");
}
.root{
-fx-font-size: 16;
-fx-font-family: "Pixeboy";
}
@font-face
可以工作但是:
它需要在您的 CSS 文件中排在第一位。
它不能在 src 中使用前导斜线 URL.
src URL 必须定位
- 网络字体通过有效的完整 URL,例如
http:
、https:
或file:
协议或 - 要将其作为资源获取,它必须是包含@font-face 指令的CSS 文件的相对 路径。
- 网络字体通过有效的完整 URL,例如
如果一切设置正确,则以下答案中的说明可以正常工作(使用 JavaFX 17 验证):
- JavaFX custom Fonts
适用于问题中定义的资源的示例 URL 是相对于 CSS 文件的 src URL:
src: url("fonts/Pixeboy.ttf");
假设字体实际上位于引用 @font-face
.
当我测试时,我使用了上面答案中的 CSS and fonts 链接。
疑难解答
如果您在 src URL 中为 @font-face 使用前导斜杠(例如
src: url("/fonts/Pixeboy.ttf");
),您将收到以下消息并且字体不会正在加载:Jan 12, 2022 2:31:09 PM javafx.css.CssParser reportException WARNING: Please report java.lang.NullPointerException at:
即使从 class/module 路径的根目录找到字体文件的路径正确,也会生成错误消息并且不会加载字体。
如果您为不存在的字体指定位置(例如
src: url("wrongfoldername/Pixeboy.ttf");
,您将收到以下错误消息并且不会加载字体:Jan 12, 2022 2:43:18 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged INFO: Could not load @font-face font [file:/<build-output-dir>/wrongfoldername/Pixeboy.ttf]