在JavaFX中使用FXML显示图像。

huangapple go评论59阅读模式
英文:

Display Image in JavaFX using fxml

问题

我一直在尝试在我的JavaFX应用程序中使用FXML显示我的图像,但不成功。

我的代码如下:

...
<ImageView fitHeight="24" fitWidth="24"
    pickOnBounds="true" preserveRatio="true">
        <Image url="@res/icon.png"/>
</ImageView>
...

我的项目目录结构如下:

在JavaFX中使用FXML显示图像。

没有错误,但我的图像没有显示。我认为图像已加载,但没有显示。

这是可能的吗?我如何检查?如果是这样,是否应该出现错误?

事实上,即使我提供一个不正确的名称,也没有运行时错误。所以也许没有加载?

我还尝试加载不同的图像并更改尺寸,但都不起作用。

我已经尝试过:

  1. "/res/icon.png" -> 运行时错误 #1
  2. "res/icon.png" -> Lint错误 + 运行时错误 #1
  3. "/../res/icon.png" -> Lint错误 + 运行时错误 #1
  4. "../res/icon.png" -> Lint错误 + 运行时错误 #1
  5. "@/res/icon.png" -> 运行时错误 #2
  6. "@res/icon.png" -> Lint错误(我正在使用的方式)
  7. "@/../res/icon.png" -> Lint错误 + 运行时错误 #2
  8. "@../res/icon.png" -> Lint错误,没有显示

运行时错误 #1: Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found

运行时错误 #2: Caused by: javafx.fxml.LoadException: Invalid resource: /res/icon.png not found on the classpath

(我知道在URL中不应该使用..或.。我只是在测试)

我看过:

  1. 为什么在JavaFX中图像不加载 这与我的配置完全相同。
  2. 如何使用ImageView显示图像 这里我找到了在FXML中链接图像的正确方式。没有错误,但图像没有加载/显示。
  3. ImageView不起作用 但我想要在FXML中_仅_执行这个操作。
  4. 无法加载JavaFX图像 同样只涉及FXML。
  5. 在JavaFX中无法加载图像 同样只涉及FXML。
  6. 将图像加载到JavaFX的ImageView 同样只涉及FXML。
  7. JavaFX图像加载错误 我没有使用CSS。
  8. JavaFX中后台和线程中加载图像 这不是我关注的内容。
  9. 如何确定Fxml文件、CSS文件、图像和其他文件的正确路径 非常详尽,但关于FXML呢?

编辑:我重新创建了原始项目的副本,并尝试了评论中的故障排除。

结果(对故障排除部分的响应):

  1. 正确的图像名称:名称有效,并且图像已正确部署到out文件夹。但是,它未被IDE识别,导致错误或未显示。

  2. 正确的路径

    a) 绝对URLs

有趣的是,getClass().getResource("/res/icon.png").getExternalForm()方法会给出:

file:/C:/Users/Chander%20Shekhar/IdeaProjects/TestRes/out/production/TestRes/res/icon.png

并且正常工作。不确定是否应该存在file:getPath()方法给出:

/C:/Users/Chander%20Shekhar/IdeaProjects/TestRes/out/production/TestRes/res/icon.png

并且不起作用(运行时错误#1)。删除第一个/会导致另一个错误:

Caused by: java.lang.IllegalArgumentException: Invalid URL: unknown protocol: c
 b) _相对URLs_

所以实际上我在我的res文件夹中创建了一个标记类,并使用了ImageMarker.class.getResource("icon.png").toExternalForm(),它给出了:

file:/C:/Users/Chander%20Shekhar/IdeaProjects/TestRes/out/production/TestRes/res/icon.png

与第一个示例相同,并且可以工作!

  1. 类路径:我还确保我使用项目根目录、src文件夹的相对地址(我认为是类路径的根目录。错误吗?)

但最终,所有这些仍然与我使用FXML无关。天啊!

英文:

I have been unsuccessfully trying to display my image in my JavaFX application using fxml.
My code:

...
<ImageView fitHeight="24" fitWidth="24"
    pickOnBounds="true" preserveRatio="true">
        <Image url="@res/icon.png"/>
</ImageView>
...

My project directory structure:
在JavaFX中使用FXML显示图像。

There is no error, but my image is not displaying. I think the image was loaded, but not displayed.
Is this possible? How can I check? If it is, shouldn't there be an error for it?

Infact, even if I give an incorrect name, there is no runtime error. So maybe not loaded?

I have also tried loading different images and changing the dimensions, nothing works.
<hr>
I have tried:

  1. &quot;/res/icon.png&quot; -> Runtime error #1
  2. &quot;res/icon.png&quot; -> Lint error + Runtime error #1
  3. &quot;/../res/icon.png&quot; -> Lint error + Runtime error #1
  4. &quot;../res/icon.png&quot; -> Lint error + Runtime error #1
  5. &quot;@/res/icon.png&quot; -> Runtime error #2
  6. &quot;@res/icon.png&quot; -> Lint error (What I am using)
  7. &quot;@/../res/icon.png&quot; -> Lint error + Runtime error #2
  8. &quot;@../res/icon.png&quot; -> Lint error, no display

Runtime Error #1: Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found

Runtime Error #2: Caused by: javafx.fxml.LoadException: Invalid resource: /res/icon.png not found on the classpath

(I know we shouldn't use .. or . in urls. I was just testing)
<hr>
I have seen:

  1. Why does Image don't load in JavaFX which is exactly my configuration.
  2. How to show image using ImageView from where I got the proper way to link images in fxml. No error, but image not loading/displaying.
  3. ImageView does not work but I would like to do this in fxml only.
  4. Can't load JavaFX Image again fxml only.
  5. Cannot load Image in JavaFX again fxml only.
  6. Load Image to ImageView JavaFX again fxml only.
  7. JavaFX Image loading error I am not using CSS.
  8. JavaFX image loading in background and threads not what I'm interested in.
  9. How to determine correct path... very extensive, but what about fxml?
    <hr>

Edit: I recreated a duplicate of the original project and tried the troubleshooting following the comments.

The results (response to troubleshooting section):

  1. Proper image name: The name was valid, and the image properly deployed to out folder. But it was not recognized by the IDE and resulted in error or no display.

  2. Proper paths

    a) Absolute URLs

Interestingly, the getClass().getResource(&quot;/res/icon.png&quot;).getExternalForm() method gives:

 file:/C:/Users/Chander%20Shekhar/IdeaProjects/TestRes/out/production/TestRes/res/icon.png

and works. Not sure if the file: should be there. The getPath() method gives:

/C:/Users/Chander%20Shekhar/IdeaProjects/TestRes/out/production/TestRes/res/icon.png

and doesn't work (Runtime error#1). Removing the first / gave another error:

Caused by: java.lang.IllegalArgumentException: Invalid URL: unknown protocol: c

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b) Relative URLs

So I actually made a marker class in my res folder, and used ImageMarker.class.getResource(&quot;icon.png&quot;).toExternalForm() which gave

file:/C:/Users/Chander%20Shekhar/IdeaProjects/TestRes/out/production/TestRes/res/icon.png

Same as the first example, and works!

  1. Classpath: I also made sure I had the relative address from project root, the src folder (which I believe is the classpath root. Wrong?)

But in the end, all of this is still irrelevant, since I am using fxml. God!

答案1

得分: 4

我建议您使用一个资源文件夹,并将您的FXML文件和图像文件都放在那个文件夹里。这样您的文件结构会是这样的:

src/
|-- main/
    | -- java/
    | -- resources/
         | -- fxml/screen.fxml
         | -- img/image.png

然后在 screen.fxml 文件中,您可以这样做:

<ImageView pickOnBounds="true" preserveRatio="true">
    <image>
        <Image url="@../img/image.png" />
    </image>
</ImageView>

这就是我组织项目的方式,它运行得很好。CSS 文件也会放在 resources 文件夹中,希望这对您有所帮助。

附注:您有一个 res/ 文件夹,但是除非您确认,Java 不会将其视为资源文件夹。如果您使用的是 IntelliJ,您可以右键单击它,然后选择 "Mark as resources root"。如果您使用 Maven,有方法可以将其声明为资源文件夹。无论如何,如果您将其重命名为 resources/,它就会自动被视为资源文件夹。

英文:

I suggest you to use a resources folder, and put both your FXML and images in that folder. So you'd have this file structure:

src/
|-- main/
    | -- java/
    | -- resources/
         | -- fxml/screen.fxml
         | -- img/image.png

Then on screen.fxml you would do something like

&lt;ImageView pickOnBounds=&quot;true&quot; preserveRatio=&quot;true&quot;&gt;
    &lt;image&gt;
        &lt;Image url=&quot;@../img/image.png&quot; /&gt;
    &lt;/image&gt;
&lt;/ImageView&gt;

That's how I organize my projects and it works just fine. CSS files will live in resources too, hope that helps.

PS: You have a res/ folder, but Java won't recognize it as a resource folder unless you acknowledge it. If you're on IntelliJ, you can right click it and then choose "Mark as resources root". If you use maven there are ways to declare it as your resource folder. Anyways, if you just rename it to resources/ it will be automatically considered as your resource folder.

huangapple
  • 本文由 发表于 2020年8月29日 18:47:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63646114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定