【问题标题】:JavaFX set image throws IllegalArgumentExceptionJavaFX 设置图像抛出 IllegalArgumentException
【发布时间】:2020-07-03 01:39:24
【问题描述】:

我目前正在为 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 会在 new Image 中自动显示“s:”,而他却说“url:”:

"s:" in image

以前有人遇到过这些问题吗?感谢您提供任何帮助,截止日期即将到来。

【问题讨论】:

  • IntelliJ 只是向您显示constructor you're calling 的参数名称,因此如果您使用不同的构造函数,参数名称不同也就不足为奇了。您可能需要检查构建文件夹(binclasses 或类似的:我不使用 IntelliJ)并确保图像与类文件一起部署。假设您有 IMAGENAME.png 或将其替换为正确的文件名,选项 4 应该可以工作。

标签: java javafx


【解决方案1】:

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

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

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

【讨论】:

  • 它将在 jar 中失败 - 您必须弄清楚资源查找的工作原理并使用它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-18
  • 2014-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多