如何在 IntelliJ IDEA 中将 AwesomeFontFX 图标包添加到 JavaFX 项目?

How to add AwesomeFontFX Icon Pack to JavaFX Project in IntelliJ IDEA?

我正在创建一个 mini-JavaFX 项目并想使用 Awesome Font Icon Pack.I 使用 Scenebuilder 加载 fontawesomefx-8.9 并在其中打开 FXML 并在其中加载两个图标,还添加了JAR 文件在 IntelliJ 的外部库中,但是当我 运行 代码时,它为 de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView 给出了 ClassNotFoundException。我该怎么办?

我删除了两个图标,代码 运行 没问题。我搜索了很多地方,但它只显示将 JAR 文件添加到外部库,什么也没有。我正在使用 JDK 12、JavaFX 12 和 Scenebuilder 11.0。

FXML 文件

<?xml version="1.0" encoding="UTF-8"?>

<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<fx:root alignment="center" hgap="10" type="GridPane" vgap="10" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Button mnemonicParsing="false" text="Click Me!!!">
         <opaqueInsets>
            <Insets left="5.0" />
         </opaqueInsets></Button>
      <FontAwesomeIconView glyphName="CIRCLE" />
   </children>
   <columnConstraints>
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
   </rowConstraints>
</fx:root>

控制器Class

package sample;

import javafx.fxml.Initializable;

import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

    }

Module-Info.java

module Practice {
    requires javafx.controls;
    requires javafx.fxml;

    opens sample;
}

主要Class

package sample;

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{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

错误

"C:\Program Files\Java\jdk-12.0.2\bin\java.exe" --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2.1\lib\idea_rt.jar=7604:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2.1\bin" -Dfile.encoding=UTF-8 -p "E:\Java Programs\javafx-sdk-12.0.2\lib\javafx.base.jar;E:\Java Programs\javafx-sdk-12.0.2\lib\javafx.graphics.jar;E:\Java Programs\JavaFX\Practice\out\production\Practice;E:\Java Programs\javafx-sdk-12.0.2\lib\javafx-swt.jar;E:\Java Programs\javafx-sdk-12.0.2\lib\javafx.controls.jar;E:\Java Programs\javafx-sdk-12.0.2\lib\javafx.fxml.jar;E:\Java Programs\javafx-sdk-12.0.2\lib\javafx.media.jar;E:\Java Programs\javafx-sdk-12.0.2\lib\javafx.swing.jar;E:\Java Programs\javafx-sdk-12.0.2\lib\javafx.web.jar;E:\Java Programs\JavaFX\fontawesomefx-8.9.jar" -m Practice/sample.Main
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: javafx.fxml.LoadException: 
/E:/Java%20Programs/JavaFX/Practice/out/production/Practice/sample/sample.fxml

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
    at javafx.fxml/javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2863)
    at javafx.fxml/javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2707)
    at javafx.fxml/javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2676)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2542)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
    at Practice/sample.Main.start(Main.java:13)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop(WinApplication.java:174)
    ... 1 more
Caused by: java.lang.ClassNotFoundException: de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2931)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2920)
    at javafx.fxml/javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2861)
    ... 20 more
Exception running application sample.Main

Process finished with exit code 1

由于您使用的是 Java 和 JavaFX 12,因此您应该使用有效的 FontAwesomeFX 版本:11.0.0.

您可以在这里找到它:https://bintray.com/jerady/maven/FontAwesomeFX/11.0.0

如果您使用 Maven 或 Gradle,您可以只包含 FontAwesome 依赖项:

dependencies {
    implementation "de.jensd:fontawesomefx-fontawesome:4.7.0-11"
}

这还将包括 de.jensd:fontawesomefx-commons:11.0

如果您不使用 Maven/Gradle,则必须从 here and here 下载它们,并将它们包含在您的项目中。

下一步:因为您有一个模块化应用程序,您的模块描述符应该包含您的项目需要的所有模块。

因此您的 module-info 应该包括 fontawesome 模块:

module Practice {
    requires javafx.controls;
    requires javafx.fxml;
    requires de.jensd.fx.fontawesomefx.fontawesome;

    opens sample to javafx.fxml;
    exports sample;
}

现在您的项目将具有所有必需的依赖项并且可以运行。