本文介绍了如何从绝对路径创建 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 图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!