我正在尝试将图像从目录读入数组列表,但是遇到了麻烦。我可能会出错,而且似乎找不到解决方法。我发现了许多其他帖子,但它们只是在读取一张指定的图片。
private ArrayList<ImageIcon> images = new ArrayList<>();
Scanner s;
try {
s = new Scanner(new File("/images"));
while(s.hasNext()) {
images.add(s.hasNext());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
假设您正在尝试读取名称在您正在读取的文本文件中的图像,这是创建ImageIcon
对象并将其添加到列表中的方法:
while(s.hasNext()) {
String imageFileName = s.next()
images.add(new ImageIcon(imageFileName));
}