javafx css使用自定义字体

2018-09-05  本文已影响479人  风少侠

方式一

在应用启动的时候加载自定义字体:

    @Override
    public void init() throws Exception {
        super.init();
        Log.isDebug = true;
        Font font1 = Font.loadFont(getClass().getResourceAsStream("/resource/font/华康海报体W12_P.TTF"), 12);
        Font font2 = Font.loadFont(getClass().getResourceAsStream("/resource/font/方正胖娃简体.TTF"), 12);
        Font font3 = Font.loadFont(getClass().getResourceAsStream("/resource/font/方正粗圆简体.TTF"), 12);
        Font font4 = Font.loadFont(getClass().getResourceAsStream("/resource/font/长城特粗圆体.TTF"), 12);
        Font font5 = Font.loadFont(getClass().getResourceAsStream("/resource/font/长城特圆体.TTF"), 12);
        Log.out(font1);
        Log.out(font2);
        Log.out(font3);
        Log.out(font4);
        Log.out(font5);
    }

日志如下:

Font[name=DFPHaiBaoW12-GB, family=DFPHaiBaoW12-GB, style=Regular, size=12.0]
Font[name=FZPangWa-M18S, family=FZPangWa-M18S, style=Regular, size=12.0]
Font[name=FZCuYuan-M03S, family=FZCuYuan-M03S, style=Regular, size=12.0]
Font[name=长城特粗圆体, family=长城特粗圆体, style=Regular, size=12.0]
Font[name=长城特圆体, family=长城特圆体, style=Regular, size=12.0]

在需要使用的地方配置-fx-font-family属性即可:

#label_more{
    -fx-font-family: "FZCuYuan-M03S";
    -fx-background-color: transparent;
    -fx-text-fill: white;
    -fx-font-size: 10pt;
    -fx-font-weight: bold;
}

方式二

@font-face {
    font-family: 'TeChu';
    src: url('/resource/font/长城特粗圆体.TTF');
}

.btnMenu .text{
    -fx-font-family: "TeChu";
    -fx-font-weight: bolder;
    -fx-font-size: 22;
    -fx-stroke: #92c854;
    -fx-stroke-width: 1;
}

参考:
How to integrate custom fonts in your JavaFX application by using CSS

上一篇 下一篇

猜你喜欢

热点阅读