设置Image javafx时运行时错误IllegalArgumentException [英] Runtime error IllegalArgumentException when setting Image javafx

查看:494
本文介绍了设置Image javafx时运行时错误IllegalArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误

Caused by: java.lang.IllegalArgumentException: Invalid URL: unknown
protocol: c at javafx.scene.image.Image.validateUrl(Image.java:1097) 
at javafx.scene.image.Image.<init>(Image.java:598)
at javafx.scene.image.ImageView.<init>(ImageView.java:164)
at fileshare_client.fx.pkg1.UploadappUI_1Controller.iconimagebuttonAction(Uploadapp‌​UI_1Controller.java:355)" java:355 

这是

imageview=new ImageView(iconimage.getAbsolutePath());"

这是我的代码:

@FXML
private AnchorPane mainAnchorpane;
@FXML
private ImageView imageview;
private File iconimage;

@FXML
public void iconimagebuttonAction(ActionEvent event) {
  FileChooser filechooser = new FileChooser();
  iconimage = filechooser.showOpenDialog(mainAnchorpane.getScene().getWindow());
  System.out.println(iconimage.getName());
    if (iconimage != null) {
      String iconimagepath = iconimage.getAbsolutePath();
      System.out.println(iconimagepath);
      **imageview=new ImageView(iconimage.getAbsolutePath());**// error
    }
}


推荐答案

使用

iconimage.getAbsolutePath()

给出文件的绝对路径,其中构造函数需要文件网址

gives you the absolute path of the file, where as the constructor expects a file URL

尝试使用

iconimage.toURI().toString()

或追加文件: 到绝对路径

"file:" + iconimage.getAbsolutePath()

这篇关于设置Image javafx时运行时错误IllegalArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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