gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:34:37 26 4
gpt4 key购买 nike

我正在创建一个迷你JavaFX项目,想要使用Awesome Font Icon Pack。我使用Scenebuilder加载了fontawesomefx-8.9,并在其中打开了FXML并在其中加载了两个图标,还在External中添加了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>

Controller 类

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;
}

主类

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$2(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$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(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$3(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 下载它们和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;
}

现在您的项目将拥有所有必需的依赖项并且可以正常工作。

关于java - 如何将 AwesomeFontFX 图标包添加到 IntelliJ IDEA 中的 JavaFX 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57641108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com