javafx.scene.image.Image(" flower.png")在哪里寻找flower.png? [英] Where does javafx.scene.image.Image("flower.png") look for flower.png?

查看:288
本文介绍了javafx.scene.image.Image(" flower.png")在哪里寻找flower.png?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道JavaFX Image构造函数的相对路径解析算法的用途:

I would like to know what the JavaFX Image constructor's relative path resolution algorithm is for:


  1. 独立应用程序。

  2. 浏览器部署的应用程序。

  3. Webstart部署的应用程序。

如果算法可能在多个位置查找,我想知道它们对于上述每个部署方案的用途。

If the algorithm may look in multiple locations, I'd like to know what they are for each of the above deployment scenarios.

如果独立应用程序的行为是当应用程序放在jar中时,我也想知道。

If the behaviour for stand-alone apps is different when the app is placed in a jar, I'd like to know that too.

推荐答案

然后用户提供相对路径 new Image(String url)它将相对于应用程序的workdir又名 System.getProperty(user.dir)

Then user provides relative path to new Image(String url) it will be relative against workdir of the app aka System.getProperty("user.dir")


  1. 对于独立应用程序,它是您从
  2. 开始申请的文件夹。
  3. For web -start应用程序从命令行运行( javaws myfxapp.jnlp ),其工作方式与独立应用程序相同

  4. 对于插件启动或者从浏览器加载的webstart应用程序,您无法可靠地确定workdir位置。这取决于浏览器,甚至是该浏览器的已安装插件。

  1. For stand-alone app it's a folder you started an application from
  2. For web-start app being run from command line (javaws myfxapp.jnlp) it works the same way as with standalone app
  3. For plugin start or webstart app loaded from browser you can't reliably be sure about workdir location. It depends on browser and even on installed plugins for that browser.

所以一般方向是:对于独立应用程序,使用 url 与在新文件(String url)中使用它的方式相同。对于jnlp / plugin,请使用带协议的完整URL或 Class.getResource()方法。

So general direction is: for standalone apps use url same way as you'll use it in new File(String url). For jnlp/plugin use full url with protocol or Class.getResource() approach.

更新: 请注意,这种行为将在2.2中得到澄清和更改。请参见 http://javafx-jira.kenai.com/browse/RT-18291

Update: Please, note, this behavior is going to be clarified and changed in 2.2. See http://javafx-jira.kenai.com/browse/RT-18291

以下评论来自RT-18291并概述了2.2的预期变化:

The following comment is from RT-18291 and outlines the intended changes for 2.2:


Martin Sladecek添加了评论 - 2012年5月15日上午10:53
在与Richard Bair讨论后,我们决定改变路径的当前(未记录的)行为。目前,它们被视为user.dir relative。所有带有或不带有前导斜杠的路径现在将被解析为相对于类路径,这将与CSS一致,更多对应于用户期望的内容。
对于user.dir相对路径,仍然可以使用file:URL。

Martin Sladecek added a comment - May, 15 2012 10:53 AM After discussion with Richard Bair, we decided to change the current (undocumented) behavior for paths. Currently, they are treated as user.dir relative. All path, with or without leading slash will be now resolved as relative to classpath, which will be consistent with CSS and more corresponds to what might users expect. For user.dir relative paths, "file:" URL can be still used.

这篇关于javafx.scene.image.Image(" flower.png")在哪里寻找flower.png?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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