且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在JavaFX中将Fo​​ntAwesome升级到版本5

更新时间:2023-02-22 19:22:16

我终于可以使用它了.除了更改字体系列之外,您还必须为JavaFX使用正确的字体文件,因为它们分发的大多数选项似乎都不起作用...

I've finally got it working. Besides changing the font-family, you must use the correct font file for JavaFX, as most of the options they distribute don't seem to work...

此外,他们还更改了图标的名称(很好,对吗?)

Also, they've changed the names of the icons (nice, right?) as shown in the upgrading docs, so you must update them one by one.

在Web发行版中,只有名为xx-solid-900.xx的字体文件有效. wofftiff扩展名都可以使用,但woff2无效.

From the web distribution, only the font files called xx-solid-900.xx work. The woff and tiff extensions both seem to work, but woff2 doesn't.

我还尝试了桌面发行版,该发行版具有更大的文件,只有Font Awesome 5 Free-Solid-900.otf可用.

I also tried the desktop distribution, which has much larger files, and only Font Awesome 5 Free-Solid-900.otf worked.

这是我在上述应用程序中显示的版本5中找到的钢笔图标(unicode f303):

Here's the pen icon (unicode f303) I found in version 5 displayed in the above app:

更犀利!

作为参考,最终的源代码:

For reference, the final source code:

import javafx.application.Application
import javafx.scene.Scene
import javafx.scene.control.Label
import javafx.scene.layout.VBox
import javafx.scene.text.Font
import javafx.stage.Stage

class App extends Application {
    static final String PENCIL = "\uf303"

    @Override
    void start(Stage primaryStage) throws Exception {
        def root = new VBox(10)
        root.children.with {
            add new Label('My icon')
            add new Label(PENCIL).with {
                it.style = '-fx-font-family: "Font Awesome 5 Free";' +
                        '-fx-font-size: 24px;' +
                        '-fx-text-fill: red'
                it
            }
        }
        def scene = new Scene(root, 100, 140)
        primaryStage.with {
            it.scene = scene
            it.title = "FontAwesome Demo"
            centerOnScreen()
            show()
        }
    }

    static void main(String[] args) {
        Font.loadFont(App.getResource("/fa-solid-900.woff").toExternalForm(), 12)
        launch(App, args)
    }
}