JavaFx Project Error : Caused by: com.sun.javafx.fxml.PropertyNotFoundException: Property "iconName" does not exist or is read-only

JavaFx Project Error : Caused by: com.sun.javafx.fxml.PropertyNotFoundException: Property "iconName" does not exist or is read-only

所以我正在尝试 运行 这个项目,它显示了这个错误,尽管我添加了 fontawesomefx-8.2.jar

 Caused by: java.lang.RuntimeException: Exception in Application start
 method     at
com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)   at com.sun.javafx.application.LauncherImpl.lambda$launchApplication9(LauncherImpl.java:182) 
    at java.lang.Thread.run(Thread.java:748)

Caused by: javafx.fxml.LoadException:  file:/C:/Users/Yasmine%20Daly/Downloads/Gaming%20Dashboard/Gaming%20Dashboard/dist/run1549224879/Gaming%20Dashboard.jar!/gaming/dashboard/UI.fxml:23

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)  at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)    at gaming.dashboard.GamingDashboard.start(GamingDashboard.java:13)  at com.sun.javafx.application.LauncherImpl.lambda$launchApplication16(LauncherImpl.java:863)     at com.sun.javafx.application.PlatformImpl.lambda$runAndWait9(PlatformImpl.java:326)     at com.sun.javafx.application.PlatformImpl.lambda$null7(PlatformImpl.java:295)   at java.security.AccessController.doPrivileged(Native Method)   at com.sun.javafx.application.PlatformImpl.lambda$runLater8(PlatformImpl.java:294)   at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)     at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)  at com.sun.glass.ui.win.WinApplication.lambda$null2(WinApplication.java:177)     ... 1 more Caused by: com.sun.javafx.fxml.PropertyNotFoundException: Property "iconName" does not exist or is read-only.    at javafx.fxml.FXMLLoader$Element.processValue(FXMLLoader.java:348)     at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:325)     at javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:235)    at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:767)   at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)   at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)    ... 17 more Exception running application gaming.dashboard.GamingDashboard Java Result: 1

这是 UI 文件,其中 iconName 存在并带有红色下划线;

<FontAwesomeIcon iconName="MUSIC" layoutX="32.0" layoutY="134.0" size="1.5em" styleClass="sidebar-icon" />
            <FontAwesomeIcon iconName="GEAR" layoutX="32.0" layoutY="175.0" size="1.5em" styleClass="sidebar-icon" />
            <FontAwesomeIcon iconName="FILE" layoutX="34.0" layoutY="210.0" size="1.2em" styleClass="sidebar-icon" />
            <FontAwesomeIcon iconName="DASHBOARD" layoutX="33.0" layoutY="242.0" size="1.2em" styleClass="sidebar-icon" />
            <FontAwesomeIcon iconName="MUSIC" layoutX="32.0" layoutY="278.0" size="1.2em" styleClass="sidebar-icon" />
            <FontAwesomeIcon iconName="HEART" layoutX="32.0" layoutY="368.0" size="1.2em" styleClass="sidebar-icon" />
            <FontAwesomeIcon iconName="GEAR" layoutX="31.0" layoutY="399.0" size="1.5em" styleClass="sidebar-icon" />

如果解决了以下问题,那么我就可以使用 FXML 中的超棒字体图标了:

  1. 如其所说,iconName 不是 FontAwesomeIcon 的 属性。
  2. FontAwesomeIcon 是枚举而不是节点,所以我不知道您将如何从 FXML 使用它。
  3. 有一个 FontAwesomeIconView,它是一个节点,有一个名为 glyphName 的 属性,可以代替使用。
  4. 确保最新版本,Java/JavaFX 17.0.2 和 fontawesomefx 4.7.0-9.1.2。

按照此处的说明进行操作:

使用以下 fxml 文件:

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

<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.HBox?>

<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="10.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <FontAwesomeIconView glyphName="MUSIC" size="1.5em" />
      <FontAwesomeIconView glyphName="GEAR" size="1.5em" />
      <FontAwesomeIconView glyphName="FILE" size="1.5em" />
      <FontAwesomeIconView glyphName="DASHBOARD" size="1.5em" />
      <FontAwesomeIconView glyphName="HEART" size="1.5em" />
   </children>
   <padding>
      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
   </padding>
</HBox>

在 SceneBuilder 中显示以下输出:

要在应用程序中使用此功能,您需要使用最新的 JDK 和 JavaFX,例如17.0.2,以及最新的 FontAwesomeFX 版本并将 FXML 加载到您的应用程序中。

<dependency>
    <groupId>de.jensd</groupId>
    <artifactId>fontawesomefx-fontawesome</artifactId>
    <version>4.7.0-9.1.2</version>
</dependency>

有关通过应用访问的示例,请参阅此问题:

  • Unable to import certain classes in fontawesomefx after updating for Java 9 / Java 11

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class FontApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(FontApplication.class.getResource("fonts.fxml"));
        stage.setScene(new Scene(fxmlLoader.load()));
        stage.show();
    }

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