将图像加载到 ImageView JavaFX
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22744283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Load image to ImageView JavaFX
提问by FilipR
I would like to display image(saved in project folder) in dialog window, but when I run my method showDialogWithImage I get FileNotFoundExcpetion: imgs\pic1.jpg (The system cannot find the file specified), although the image is located there.
我想在对话框窗口中显示图像(保存在项目文件夹中),但是当我运行我的方法 showDialogWithImage 时,我得到 FileNotFoundExcpetion: imgs\pic1.jpg(系统找不到指定的文件),尽管图像位于那里。
I have tried load image on this way too:
Image image = new Image(getClass().getResourceAsStream(path));, but got the same problem.
我也尝试过以这种方式加载图像:
Image image = new Image(getClass().getResourceAsStream(path));,但遇到了同样的问题。
Are there some others possibilities to load image to ImageView ?
Thank you for help!
还有其他一些可能性将图像加载到 ImageView 吗?
谢谢你的帮助!
My Java code is located in src\myProject\gui in project folder.
path="imgs\pic1.jpg" // imgs is located in project folder
我的 Java 代码位于项目文件夹中的 src\myProject\gui 中。
path="imgs\pic1.jpg" // imgs 位于项目文件夹中
public void showDialogWithImage(String path) {
final Stage dialogStage = new Stage();
logger.info(path);
InputStream is = null;
try {
is = new FileInputStream(path); // here I get FileNotFoundException
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Image image = new Image(is);
ImageView view = new ImageView();
view.setImage(image);
Button btnOK = new Button("OK");
btnOK.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
dialogStage.close();
}
});
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.setScene(new Scene(VBoxBuilder.create()
.children(view, btnOK).alignment(Pos.CENTER)
.padding(new Insets(35)).build()));
dialogStage.show();
}
采纳答案by Paul Samsotha
getClass().getResourceAsStream(path)
will start its file search from the location of the calling class. So by using this path "imgs\pic1.jpg"
, you're saying this is your file structure
getClass().getResourceAsStream(path)
将从调用类的位置开始其文件搜索。所以通过使用这个路径"imgs\pic1.jpg"
,你说这是你的文件结构
src\myProject\gui\imgs\pic1.jpg
To have the search traverse back, you need the extra separator before imgs
. So
要返回搜索,您需要在imgs
. 所以
"\imgs\pic1.jpg"
Also, I think when you use a back slash as separator, you need to escape it. So
另外,我认为当您使用反斜杠作为分隔符时,您需要将其转义。所以
"\imgs\pic1.jpg
Or just use forward slash
或者只使用正斜杠
"/imgs/pic1.jpg
Another option is to use a class loader, that will search from the root, where you don't need the beginning separator
另一种选择是使用类加载器,它将从根搜索,您不需要开始分隔符
getClass().getClassLoader().getResourceAsStream("imgs/pic1.png");
回答by Menai Ala Eddine
when you load an image with path you need replace seperator file "\" with "/" for example
例如,当您加载带有路径的图像时,您需要将分隔符文件“\”替换为“/”
String ImageName="MyImage.png";
File file = new File("src\Messages\Attachements\Images");
try {
if (!file.exists()) {
FileUtils.forceMkdir(file);
}
}
} catch (Exception io) {
io.printStackTrace();
}
Image image = new Image("/Messages/Attachements/Images/"+ImageName=);
ImageReceived.setImage(image);