image - 设置 Image javafx 时出现运行时错误 IllegalArgumentException

标签 image url javafx runtime-error invalid-url

我收到这个错误

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()

为您提供文件的绝对路径,其中构造函数期望 file URL

尝试使用

iconimage.toURI().toString()

或将 file: 附加到绝对路径

"file:" + iconimage.getAbsolutePath()

关于image - 设置 Image javafx 时出现运行时错误 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25646263/

相关文章:

c# - C#中基于向导控件的ActiveStepIndex修改QueryString

java - 我可以使用什么 java 库来比较两个 URL 是否相等?

java - 复选框状态下的 TreeView 传输

JavaScript:单击按钮时简单加载图像

css - 如何使菜单链接在文本旁边有图像?

java - 我应该使用哪个 Java 类来表示 Git SSH URL?

drag-and-drop - TreeItem 是否支持拖放?

java - 按下 javafx 按钮时不执行

image - 在 MATLAB 中将图像绘制为轴标签

java - BufferedImage/ImageIcon 空指针异常