我在我的javafx项目中有2个包,第一个"图像"对于图像和第二个java代码,我想问你如何在这种情况下设置图像路径我尝试了serval解决方案,但没有任何效果。
尝试代码:
reg_image_statut.setImage(new Image("src/images/denided.png"));
reg_image_statut.setImage(new Image("/src/images/denided.png"));
reg_image_statut.setImage(new Image("/images/denided.png"));
reg_image_statut.setImage(new Image("../images/denided.png"));
例外情况:
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1089)
at javafx.scene.image.Image.<init>(Image.java:598)
解决方案: 是的@mipa解决方案很简单,它就像你说的那样,正确的解决方案是我忘记尝试的唯一可能性。谢谢@mipa
reg_image_statut.setImage(new Image("images/actived.png"));
答案 0 :(得分:3)
为什么不将它留给Java来构造Image构造函数所期望的正确URL?
Image icon16 = new Image(getClass().getResource("/images/mpgafor_logo_16x16.png").toExternalForm());
这适合我。
答案 1 :(得分:2)
如果src
是您的构建路径的源文件夹,那么您几乎可以获得它。我猜你错过了这个:
reg_image_statut.setImage(new Image("images/denided.png"));
如果仍然无效,您可以尝试其中一种方法(参见Image JavaDoc):
// The image is located in default package of the classpath
Image image1 = new Image("/flower.png");
// The image is located in my.res package of the classpath
Image image2 = new Image("my/res/flower.png");
// The image is located in the current working directory
Image image4 = new Image("file:flower.png");