Imageview in Javafx : Invalid URL

我是研究僧i 提交于 2021-02-08 10:06:50

问题


In am trying to run the following code and it throws an 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;
}

I get the following error

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

The main simply calls Receta's constructor and there, the exception is thrown, i have checked and changed several times the url of the image just in case it was wrong but it doesn't seem so.


回答1:


Strings provided to an ImageView or Image constructor specify either a URL (which includes a protocol) or, if there is no protocol, a classpath location.

All URLs supported by URL can be passed to the constructor. If the passed string is not a valid URL, but a path instead, the Image is searched on the classpath in that case.

Because you did not supply a protocol, with new ImageView("/Recetas/src/lupita.png"), then the application is looking for the image in the classpath. Your classpath isn't going to include your source directory.

If lupita.png is on the root of your classpath, then you can load it using:

new ImageView("/lupita.png");

If you want to load lupita off the filesystem using a file protocol rather than from the classpath, then you can load it using an absolute path (assuming /Recatas is a subdirectory off the root of your current filesystem):

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

Loading from an absolute path from your local filesystem is not recommended for distribution of your application as the file probably won't be in the same absolute location on somebody else's system.



来源:https://stackoverflow.com/questions/29670013/imageview-in-javafx-invalid-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!