使用JavaFX 8和css加载自定义字体

gam*_*r95 9 css fonts javafx custom-font javafx-8

问题

我正在为我的应用程序创建一个超级马里奥主题css样式表,我想将我的按钮字体更改为名为Press Start 2P的自定义字体,这是NES Mario游戏中使用的像素化字体.但是,由于某些奇怪的原因,我的应用程序没有加载字体.

CSS代码:

.text
{
    -fx-font-family: "Press-Start-2P";
}

.button
{
    -fx-background-color: transparent;
    //-fx-text-fill: rgb(255, 255, 255);
}

@font-face
{
    font-family: Press-Start-2P;
    src: url("PressStart2P.ttf");
}
Run Code Online (Sandbox Code Playgroud)

当我运行应用程序时,它给了我这样的信息:

2015年4月25日上午8:22:32 com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged INFO:无法加载@ font-face字体[file:/ C:/ Users/Ahmad%20El-Baher/Documents/Programming/Java /Integrative%20Project%20Semester%204/trunk/build/classes/res/styles/PressStart2P.ttf]

如何让我的应用程序正确加载我的字体?

Mr0*_*son 16

请注意,以下内容仅适用于 Java版本> = 1.8u60

我遇到了同样的问题.加载方式CSSJavaFX图8是由设置字体文件面,然后使用字体名称作为参考,可以通过预览的字体被发现.

字体文件是相对于CSS.我有根设置字体,但您可以删除它并保留每个节点.

CSS代码

@font-face {
    src: url("freeuniverse2.ttf"),
}

.root {
    -fx-background-color: gray;
    -fx-font-family: "Free Universe 2";
}

#window {
    -fx-border-image-source: url("images/windowfill.png");
    -fx-border-image-slice: 17 6 13 6 fill;
    -fx-border-image-width: 17 6 13 6 ;
    -fx-background-color: gray;
}

.text {
    -fx-text-fill: white;
    -fx-font-size: 16px;
    -fx-font-family: "Free Universe 2";
    -fx-background-color: transparent;
}

.pane {
    -fx-border-image-source: url("images/windowfill.png");
    -fx-border-image-slice: 17 6 13 6 fill;
    -fx-border-image-width: 17 6 13 6 ;
    -fx-background-color: gray;
}

.vbox {
        -fx-border-image-source: url("images/windowfill.png");
    -fx-border-image-slice: 17 6 13 6 fill;
    -fx-border-image-width: 17 6 13 6 ;
    -fx-background-color: gray;
}

#title-text {
    -fx-text-fill: white;
    -fx-font-size: 36px;
    -fx-font-family: "Free Universe 2";
}

.label {
    -fx-text-fill: white;
    -fx-font-size: 16px;
    -fx-font-family: "Free Universe 2";
    -fx-background-color: transparent;
}
Run Code Online (Sandbox Code Playgroud)

字体文件预览(下面你可以看到真正的字体名称)

字体文件预览


Dus*_*gal 1

查看您的 CSS 文件和 TTF 文件是否位于同一目录中。根据您的代码,src: url("PressStart2P.ttf");它们应该位于同一目录中。

另外,尝试更换

@font-face
{
    font-family: Press-Start-2P;
    src: url("PressStart2P.ttf");
}
Run Code Online (Sandbox Code Playgroud)

@font-face
{
    -fx-font-family: 'Press-Start-2P';
    src: url('PressStart2P.ttf');
}
Run Code Online (Sandbox Code Playgroud)