首页 > 解决方案 > JavaFX 设置图像抛出 IllegalArgumentException

问题描述

我目前正在为 uni 工作,我的任务是使用 Java/JavaFX 开发股票管理系统。

我的工作目录如下所示:

-src
    -assets
        -image.png
    -GUI
        -viewProduct.fxml
        -viewProductController.class

这是我的 viewProduct.fxml 中的代码:

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

<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane fx:id="adminPanelPage" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="525.0" prefWidth="514.0" style="-fx-background-color: #3f3f3f;" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.banton.GUI.viewProductController">
   <children>
      <ImageView fx:id="imgProductImage" fitHeight="175.0" fitWidth="175.0" layoutX="31.0" layoutY="37.0" pickOnBounds="true" preserveRatio="true">
          <image>
              <Image url="@../productIMGs/imageNotFound.png" />
          </image>
      </ImageView>
      <Label layoutX="223.0" layoutY="72.0" text="Product ID: " textFill="WHITE">
         <font>
            <Font name="System Bold" size="14.0" />
         </font></Label>
      <Label layoutX="223.0" layoutY="99.0" text="Product Name:" textFill="WHITE">
         <font>
            <Font name="System Bold" size="14.0" />
         </font>
      </Label>
      <Label layoutX="223.0" layoutY="127.0" text="Product Type:" textFill="WHITE">
         <font>
            <Font name="System Bold" size="14.0" />
         </font>
      </Label>
      <Label layoutX="223.0" layoutY="155.0" text="Price: " textFill="WHITE">
         <font>
            <Font name="System Bold" size="14.0" />
         </font>
      </Label>
      <Label layoutX="31.0" layoutY="236.0" text="Product Description:" textFill="WHITE">
         <font>
            <Font name="System Bold" size="14.0" />
         </font>
      </Label>
      <TextArea fx:id="txtProductDescription" editable="false" layoutX="31.0" layoutY="263.0" prefHeight="71.0" prefWidth="454.0" promptText="This is where the description text will be" style="-fx-background-color: #3f3f3f;" wrapText="true">
         <font>
            <Font size="13.0" />
         </font>
      </TextArea>
      <ImageView fitHeight="107.0" fitWidth="175.0" layoutX="170.0" layoutY="440.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../assets/editProduct.png" />
         </image>
      </ImageView>
      <ImageView fitHeight="43.0" fitWidth="59.0" layoutX="442.0" layoutY="16.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../assets/print.png" />
         </image>
      </ImageView>
      <Label fx:id="txtProductID" layoutX="308.0" layoutY="72.0" prefHeight="20.0" prefWidth="187.0" text="&lt;&lt;product id&gt;&gt;" textFill="WHITE">
         <font>
            <Font size="14.0" />
         </font>
      </Label>
      <Label fx:id="txtProductName" layoutX="332.0" layoutY="99.0" prefHeight="20.0" prefWidth="166.0" text="&lt;&lt;product  name&gt;&gt;" textFill="WHITE">
         <font>
            <Font size="14.0" />
         </font>
      </Label>
      <Label fx:id="txtProductType" layoutX="325.0" layoutY="127.0" prefHeight="20.0" prefWidth="166.0" text="&lt;&lt;product  type&gt;&gt;" textFill="WHITE">
         <font>
            <Font size="14.0" />
         </font>
      </Label>
      <Label fx:id="txtPrice" layoutX="273.0" layoutY="155.0" prefHeight="20.0" prefWidth="166.0" text="&lt;&lt;Price&gt;&gt;" textFill="WHITE">
         <font>
            <Font size="14.0" />
         </font>
      </Label>
      <Label layoutX="117.0" layoutY="358.0" text="Total Stock Level:" textFill="WHITE">
         <font>
            <Font name="System Bold" size="18.0" />
         </font>
      </Label>
      <Label fx:id="txtStockLevel" layoutX="277.0" layoutY="358.0" text="&lt;&lt;Stock Level&gt;&gt;" textFill="WHITE">
         <font>
            <Font name="System Bold" size="18.0" />
         </font>
      </Label>
      <Label layoutX="113.0" layoutY="385.0" text="Total Stock Value:" textFill="WHITE">
         <font>
            <Font name="System Bold" size="18.0" />
         </font>
      </Label>
      <Label fx:id="txtStockValue" layoutX="277.0" layoutY="385.0" text="&lt;&lt;Stock Value&gt;&gt;" textFill="WHITE">
         <font>
            <Font name="System Bold" size="18.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

但是,我正在尝试使用以下代码根据所选项目更改图像(imgProductImage):

Image image = new Image("productIMGs/imageNotFound.png");
imgProductImage.setImage(image);

然而由于某种原因,我收到以下错误:

java.lang.IllegalArgumentException: Invalid URL or resource not found

我尝试了以下方法:

  1. 在项目目录周围移动图像文件 - 包括 src 之外
  2. 使用Image image = new Image(getClass().getResourceAsStream("/assets/IMAGENAME.png"))
  3. 我什至从另一个学生那里复制了确切的代码来测试不起作用的功能。
  4. 使用Image image = new Image(getClass().getResource("/assets/IMAGENAME.png"))

因此,我怀疑这可能是 IntelliJ 问题?

我确实注意到的一件事是 IntelliJ 自动在其中显示“s:”,new Image而他说“url:”:

图像中的“s:”

以前有人遇到过这些问题吗?任何帮助表示赞赏,截止日期很快就要到了。

标签: javajavafx

解决方案


刚刚找到解决方案,对于其他有同样问题的人:

Image image = new Image("file:IMAGENAME.png")

仍然不清楚为什么简单地放置 URL 不起作用,但这似乎是一个很好的解决方案。


推荐阅读