将图像读取到ImageIcon数组列表中

问题描述 投票:0回答:1

我正在尝试将图像从目录读入数组列表,但是遇到了麻烦。我可能会出错,而且似乎找不到解决方法。我发现了许多其他帖子,但它们只是在读取一张指定的图片。

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();
        }
java user-interface jlabel imageicon
1个回答
0
投票

假设您正在尝试读取名称在您正在读取的文本文件中的图像,这是创建ImageIcon对象并将其添加到列表中的方法:

        while(s.hasNext()) {
            String imageFileName = s.next()
            images.add(new ImageIcon(imageFileName));
        }
© www.soinside.com 2019 - 2024. All rights reserved.