gpt4 book ai didi

java - 如何使用java中的扫描仪从数组列表中随机选择图像

转载 作者:行者123 更新时间:2023-12-01 14:32:13 24 4
gpt4 key购买 nike

我正在尝试扫描图像文件夹并将它们添加到数组列表中。我想结合扫描仪和随机变量来从数组列表中随机选择一个图像并将其附加到私有(private) ImageIcon 图标。 “FemaleFaces”是包含图像的目录。到目前为止,这是我的代码,感谢您的帮助!

private ImageIcon iconex; 

File f = new File("FemaleFaces");
Scan = new Scanner(f);
ArrayList<ImageIcon> files = new ArrayList<ImageIcon>();
while(Scan.hasNext())
{
files.add(new ImageIcon(Scan.next()));
}
Scan.close();

int Ffindex = new Random().nextInt(files.size());
iconex = files.get(Ffindex);

线程“main”中出现异常 java.io.FileNotFoundException:FemaleFaces(访问被拒绝) 在 java.io.FileInputStream.open( native 方法) 在 java.io.FileInputStream.(来源未知) 在 java.util.Scanner.(来源不明) 在人类。(Human.java:66) 在 testerobjects.main(testerobjects.java:19)

最佳答案

Scan.next() 返回一个String。您需要根据该值构造一个 ImageIcon

类似的东西......

files.add(new ImageIcon(Scan.next()));

关于java - 如何使用java中的扫描仪从数组列表中随机选择图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16782926/

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