gpt4 book ai didi

java - ImageView 找不到图片网址

转载 作者:行者123 更新时间:2023-11-29 04:29:48 26 4
gpt4 key购买 nike

我在 GridPane 中显示图像时遇到问题。我收到输入流不能为空的错误。异常在 ImageView 创建对象的第一行。如您所见,图片在文件夹中。有什么问题。

    ImageView windows = new ImageView(new Image(Main.class.getResourceAsStream("res/windows.png")));
ImageView linux = new ImageView(new Image(Main.class.getResourceAsStream("res/windows.png")));
ImageView mac = new ImageView(new Image(Main.class.getResourceAsStream("res/windows.png")));
ImageView android= new ImageView(new Image(Main.class.getResourceAsStream("res/windows.png")));

GridPane gpanel = new GridPane();
gpanel.setPadding(new Insets(5));
gpanel.add(windows, 0, 0);
gpanel.add(linux, 1, 0);
gpanel.add(mac, 0, 1);
gpanel.add(android, 1, 1);

enter image description here

最佳答案

那是因为找不到图片,需要去掉路径开头的res,例如:

这个:"res/windows.png" -> 应该是:"/windows.png" ...等等。

因为 getResourceAsStream(String name) 通过搜索与类关联的资源找到具有给定名称的资源,它从项目的根开始,但是因为 res 是不是另一个包,所以你不需要将它添加到相对路径的开头。

此外,假设您在另一个名为 anotherPackage 的包中有 windows.png,那么您可以从您的 main 类访问它(在你的例子中)像这样:"/anotherPackage/windows.png".

关于java - ImageView 找不到图片网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44268383/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com