【发布时间】:2022-01-13 12:11:32
【问题描述】:
我正在尝试使用 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";
}
【问题讨论】:
-
字体文件的路径显然是错误的。 (
src是源文件夹;它通常在运行时不可用) -
请遵守 java 命名约定(包名应全部小写)