java 导入图像文件,将其添加到ArrayList,然后依次显示图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10263861/
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
Importing an image file, adding it to an ArrayList, and then displaying the images sequentially
提问by James Liddycoat
I'm trying to import an image file using JFileChooser
add the image to an ArrayList
. Then finally have display the images sequentially on a JPanel
when a button is pressed. Does anyone know how I could do this? I'm new to java.
我正在尝试使用JFileChooser
将图像添加到ArrayList
. 然后最后在JPanel
按下按钮时按顺序显示图像。有谁知道我怎么能做到这一点?我是 Java 新手。
Thanks!
谢谢!
回答by I82Much
Break it down into steps.
把它分解成几个步骤。
Task 1: Import an image file using JFileChooser. Get the file object chosen Use ImageIO.read(file) to get a buffered image.
任务 1:使用 JFileChooser 导入图像文件。获取选择的文件对象 使用ImageIO.read(file) 获取缓冲图像。
Task 2: Add image to an array list
任务 2:将图像添加到数组列表
List<BufferedImage> images = new ArrayList<BufferedImage>()
images.add(image);
Task 3: Display an image in a JPanel
任务 3:在 JPanel 中显示图像
JPanel p = new JPanel();
JButton button = new JButton();
ImageIcon icon = new ImageIcon(images.get(0));
button.setIcon(icon);
p.add(button);
Task 4: Adding buttons that advance through your images
任务 4:添加在图像中前进的按钮
JButton advance = new JButton();
advance.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
advanceImage();
}
};