使用JFileChooser上传多个图像 [英] Uploading multiple images with JFileChooser

查看:110
本文介绍了使用JFileChooser上传多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在网络上搜索此问题的帮助,但找不到任何答案。此时我有一个工作程序,允许您选择一个图像文件并显示它。我想通过允许用户选择多个文件然后选择要显示的文件来构建此基础。例如,用户将选择图像并将其显示。然后,他可以单击一个按钮,允许其他图像排队等待,并且可能会显示一个按钮,点击后,图像将加载到框架中。任何人都可以指导我如何实施这个方向吗?非常感谢你的帮助!

I tried searching for help to this question all over the web and cannot find any answers. I have a working program at this point that allows you to select an image file and display it. I would like to build upon this by allowing the user to select multiple files and then choose which one to display. For example a user would select an image and it would be displayed. He could then click a button that would allow another image to be queued up and perhaps a button would show up that upon clicking, the image would load in the frame. Could anyone guide me in the right direction for how this may be implemented? Thank you so much for any help!

这是我目前的代码实现:

Here is my current code implementation:

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.util.ArrayList;



public class Images extends JFrame{
    JButton button;
    JButton button2;
    JButton button3;
    JLabel label;
    ArrayList<File> images = new ArrayList<>();
    int selectedImageIndex =0;

    public Images(){
    super("Java Image Selector");
    button = new JButton("Select");
    button.setBounds(300,300,100,40);
    button2 = new JButton("Next Image");
    button2.setBounds(250,250,100,40);
    button3 = new JButton("Previous Image");
    button3.setBounds(350,350,100,40);
    label = new JLabel();
    label.setBounds(10,10,670,250);
    add(button);
    add(button2);
    add(button3);
    add(label);

    button.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {



          JFileChooser file = new JFileChooser();
          file.setMultiSelectionEnabled(true);
          File selected[] = file.getSelectedFiles();
          file.setCurrentDirectory(new File(System.getProperty("user.home")));
          //filter the files
          FileNameExtensionFilter filter = new FileNameExtensionFilter("*.Images", "jpg","gif","png");
          file.addChoosableFileFilter(filter);
          int result = file.showSaveDialog(null);
           //if the user click on save in Jfilechooser
          if(result == JFileChooser.APPROVE_OPTION){
              File selectedFile = file.getSelectedFile();
              String path = selectedFile.getAbsolutePath();
              File[] files = file.getSelectedFiles();
              for (File open : files) {
                  images.add(open);
              }
              startProgram(selectedImageIndex);
          }
           //if the user click on save in Jfilechooser

          else if(result == JFileChooser.CANCEL_OPTION){
              System.out.println("No File Selected");
          }
        }
    });

    button2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
         goToNextImages();
      } 
    });

    button3.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
        goToPreviousImage();
      } 
    });



    setLayout(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setSize(700,400);
    setVisible(true);
    }

     // Methode to resize imageIcon with the same size of a Jlabel
    public ImageIcon ResizeImage(String ImagePath)
    {
        ImageIcon MyImage = new ImageIcon(ImagePath);
        Image img = MyImage.getImage();
        Image newImg = img.getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_SMOOTH);
        ImageIcon image = new ImageIcon(newImg);
        return image;
    }

    public void setImage(int index){

      if (index<images.size()) {
        File imageFile = images.get(index);
        String path = imageFile.getAbsolutePath();
        selectedImageIndex = index;
        label.setIcon(ResizeImage(path));
        //read the file and set the image
      }else{
        System.out.println("no image found");
      }

    }

   public void goToNextImages(){
      ++selectedImageIndex;
      setImage(selectedImageIndex);
   }

   public void goToPreviousImage(){
      --selectedImageIndex;
       setImage(selectedImageIndex);
   }

   public void startProgram(int index){
       setImage(index);
   }

    public static void main(String[] args){
        new Work();
    }
   }


推荐答案

ArrayList<File> images = new ArrayList<>();
int selectedImageIndex =0;

public void selectFiles() {
    //you can call this method multiple times it wont matter
    //files selected at each time will be stored on the images arraylist
    JFileChooser chooser = new JFileChooser();
    chooser.setMultiSelectionEnabled(true);
    chooser.showOpenDialog(this);
    File[] files = chooser.getSelectedFiles();
    for (File file : files) {
        images.add(file);
    }
    startProgram(selectedImageIndex);
}

public void setImage(int index){

  if (index<images.size()) {
        File imageFile = images.get(index);
        selectedImageIndex = index;
        //read the file and set the image
   }else{
      System.out.println("no image found");
  }

}

public void goToNextImages(){
    ++selectedImageIndex;
    setImage(selectedImageIndex);
}

public void goToPreviousImage(){
    --selectedImageIndex;
    setImage(selectedImageIndex);
}

public void startProgram(int index){
    setImage(index);
}

这篇关于使用JFileChooser上传多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆