【问题标题】:Imageview in Javafx : Invalid URLJavafx 中的图像视图:无效的 URL
【发布时间】:2015-04-16 09:02:30
【问题描述】:

在我试图运行以下代码时,它会抛出一个 IllegalArgumentException

public class Receta {
private final StringProperty nombre;
private final StringProperty dificultad;
private final StringProperty tipo;
private final StringProperty plato;
private final StringProperty ingredientes;
private final StringProperty observaciones;
private final StringProperty[] pasos = new StringProperty[10];
private final StringProperty comensales;
private final StringProperty tPrep;
private final StringProperty tCoc;
private final IntegerProperty valoracion;
private HBox caja = new HBox(3);
private ImageView imagen;
private VBox cajaV = new VBox(2);
private HBox cajaDif = new HBox(2);;

public Receta(String n){
    nombre = new SimpleStringProperty(n);
    dificultad = new SimpleStringProperty("Normal");
    tipo = new SimpleStringProperty("Primero");
    plato = new SimpleStringProperty("Carne");
    ingredientes = new SimpleStringProperty("Ninguno");
    observaciones = new SimpleStringProperty("Nada");
    comensales = new SimpleStringProperty("2");
    tPrep = new SimpleStringProperty("180");
    tCoc = new SimpleStringProperty("20");
    valoracion = new SimpleIntegerProperty(4);
    imagen = new ImageView("/Recetas/src/lupita.png"); //The exception is thrown here
    cajaDif.getChildren().addAll(null,new Label("Normal"));
    cajaV.getChildren().addAll(cajaDif,new Label(tPrep.get()));
    caja.getChildren().addAll(imagen,new Label(n),cajaV);
}

private ImageView imagenDif(){
    String d = dificultad.get();
    if(d.equals("Facil")) return new ImageView(new Image("../../facil.png"));
    if(d.equals("Medio")) return new ImageView(new Image("../../medio.png"));
    return new ImageView(new Image("../../dificil.png"));
}

public String getNombre(){
    return nombre.get();
}
public void setNombre(String n){
    nombre.set(n);
}

public String getDif(){
    return dificultad.get();
}
public void setDif(String d){
    dificultad.set(d);
}

public String getTipo(){
    return tipo.get();
}
public void setTipo(String t){
    tipo.set(t);
}

public String getPlato(){
    return plato.get();
}
public void setPlato(String p){
    plato.set(p);
}

public String getIngr(){
    return ingredientes.get();
}
public void setIng(String i){
    ingredientes.set(i);
}

public String getObs(){
    return observaciones.get();
}
public void setObs(String o){
    observaciones.set(o);
}

public String getPaso(int i){
    return pasos[i].get();
}
public void setPaso(int i, String p){
    pasos[i].set(p);
}

public String getCom(){
    return comensales.get();
}
public void setCom(String c){
    comensales.set(c);
}

public String getTPrep(){
    return tPrep.get();
}
public void setTPrep(String t){
    tPrep.set(t);
}

public String getTCoc(){
    return tCoc.get();
}
public void setTCoc(String p){
    tCoc.set(p);
}

public int getVal(){
    return valoracion.get();
}
public void setVal(int v){
    valoracion.set(v);
}

public HBox getCaja(){
    return caja;
}

我收到以下错误

Exception in Application constructor
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class recetas.MainRecetas
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/1323468230.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$158(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/934330049.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/97828179.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/237061348.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Unknown Source)
at javafx.scene.image.Image.<init>(Unknown Source)
at javafx.scene.image.ImageView.<init>(Unknown Source)
at recetas.model.Receta.<init>(Receta.java:41)
at recetas.MainRecetas.<init>(MainRecetas.java:20)
... 18 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
... 23 more
Exception running application recetas.MainRecetas

主要只是简单地调用Receta's constructor,然后抛出异常,我已经检查并更改了几次图像的url,以防万一它是错误的,但似乎并非如此。

【问题讨论】:

  • 图片在哪里?是在项目内部还是在文件系统上?
  • 试试:new ImageView("lupita.png");

标签: javafx imageview illegalargumentexception


【解决方案1】:

提供给 ImageView 或 Image constructor 的字符串指定 URL(包括协议),如果没有协议,则指定类路径位置。

URL 支持的所有 URL 都可以传递给构造函数。如果传递的字符串不是有效的 URL,而是路径,则在这种情况下,将在类路径上搜索图像。

因为您没有提供协议,new ImageView("/Recetas/src/lupita.png"),所以应用程序正在类路径中寻找图像。您的类路径不会包含您的源目录。

如果 lupita.png 位于类路径的根目录中,则可以使用以下方式加载它:

new ImageView("/lupita.png");

如果您想使用文件协议而不是从类路径从文件系统加载 lupita,那么您可以使用绝对路径加载它(假设 /Recatas 是当前文件系统根目录下的子目录):

 new ImageView("file:///Recatas/src/lupita.png");

不建议从本地文件系统的绝对路径加载应用程序,因为该文件可能不会位于其他人系统上的相同绝对位置。

【讨论】:

  • 何塞,如果答案正确并帮助解决了您的问题,请点击答案旁边的复选框,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-11
  • 1970-01-01
  • 1970-01-01
  • 2015-04-13
  • 2011-04-30
  • 2013-08-26
相关资源
最近更新 更多