在Java FX中定义图像的相对路径 [英] Define a relative path of image in Java FX

查看:1839
本文介绍了在Java FX中定义图像的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这已被多次询问过,我已经广泛搜索过这个可能很简单的问题的解决方案。我正在尝试在Oracle网站上关注简单的javaFX组件教程。我可以这样定义图像:

I know this has been asked many times and I have searched far and wide for a solution to this probably simple problem. I'm trying to follow the simple javaFX component tutorials on Oracle's website. I can define an image this way:

Image img = new Image("images/portal.png", 50, 50, true, true);

当图像位于src文件夹内的文件夹中时,这是有效的,但我正在尝试当我将图像文件夹放在src文件夹之外时,让它找到图像,如下所示:

This works when the image is in a folder inside the "src" folder, but I'm trying to get it to find the image when I have the image folder outside of the "src" folder, like this:

http://puu.sh/drF7K/bbf1a047aa.png

我该如何制作这项工作?我得到的只是无效的URL或资源未找到的错误。我试图使用绝对路径,尝试在其前面放置..,尝试HS-Graph / images / portal.png以及其中的所有内容:(谢谢!

How can I make this work? All I get is errors saying "Invalid URL or resource not found". I've tried to use the absolute path, tried putting ".." infront of it, tried "HS-Graph/images/portal.png" and everything inbetween :( Thank you!

推荐答案

我将回答我自己的问题,因为我确实找到了解决方法!我的解决方案是在指定路径时使用file:前缀。所以:

I'm going to answer my own question as I actually found a solution to this! My solution is to use the "file:" prefix when specifying a path. So:

Image img = new Image("file:images/portal.png");

当图片文件位于我的 src 文件夹之外时,效果非常好!

Works perfectly when the image file is outside of my src folder!

这篇关于在Java FX中定义图像的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆