如何从绝对路径创建 JavaFX 图像? [英] How to create a JavaFX Image from an absolute path?

查看:26
本文介绍了如何从绝对路径创建 JavaFX 图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实例化的 File 对象.我知道它包含图片格式.我不知道文件在系统上的哪个位置,除了我可用的 File 方法getPath(), getAbsolutePath() 等.

I have an instantiated File object. I know it contains a picture format. I don't know where on the system the file is placed, other than the methods of File available to me getPath(), getAbsolutePath() etc.

我的问题是:如何使用 File 中的图片实例化 JavaFX Image 对象?

My question is: how can I instantiate a JavaFX Image object with the picture in my File?

推荐答案

File 提供了一种方法来检索文件的 URL 和 Image 构造函数需要一个URL String.

File provides a method to retrieve the URL for the file and the Image constructor expects a URL String.

Image imageForFile = new Image(file.toURI().toURL().toExternalForm());

这篇关于如何从绝对路径创建 JavaFX 图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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