简体   繁体   English

如何以Java Swing形式从目录添加数字图像?

[英]How do i add a number images from a directory in a java swing form?

For my project I need to show a number of images from a file directory in a java swing form. 对于我的项目,我需要以java swing形式显示文件目录中的许多图像。 The directory and hence the number of images and their path change on every execution. 每次执行时,目录以及图像数量及其路径都会更改。 I am planning to show a fixed number of images say 5 in the form and provide a button to load subsequent images in the same place.The image filenames are sequential(like eg1.jpg,eg2.jpg, eg3.jpg...) I am using Netbeans 6.9.1 IDE 我打算在表单中显示固定数量的图像(例如5),并提供一个按钮以将后续图像加载到同一位置。图像文件名是连续的(例如eg1.jpg,eg2.jpg,eg3.jpg ...)我正在使用Netbeans 6.9.1 IDE

  1. Parse the list of files in the directory. 解析目录中的文件列表。
  2. Loop through the list and display up to the first five. 循环浏览列表并显示前五个列表。
  3. Add a button if more than 5 images exist. 如果存在5张以上图片,请添加按钮。

There are several ways to add images to a container in Swing. 有几种方法可以将图像添加到Swing中的容器。 One of the easiest is to just add it as an icon to a JLabel . 最简单的方法之一就是将其作为图标添加到JLabel I strongly recommend doing this by hand rather than using a GUI tool. 我强烈建议您手动执行此操作,而不要使用GUI工具。

i have just the solution for you. 我为您提供解决方案。 i worked on a similar project and found this link extremely helpful for displaying the images. 我在一个类似的项目中工作,发现链接对于显示图像非常有帮助。 download, run and understand the "IconDemo" project. 下载,运行并了解“ IconDemo”项目。 note that there are 2 separate threads in this project- the SwingWorker to load images (cos this is a resource intensive task) and the EventDispatchThread which concerns the GUI. 请注意,该项目中有2个独立的线程-SwingWorker加载图像(这是一项资源密集型任务)和与GUI有关的EventDispatchThread。

as for loading pictures from a directory, you can integrate the above project with a JFileChooser. 至于从目录加载图片,您可以将上述项目与JFileChooser集成。 here is my code below: 这是我的代码如下:

    //return file (image) names in the chosen directory
    public ArrayList<String> getFileNames() 
    {
        //widget to let users select a directory or file
        JFileChooser chooser = new JFileChooser();
        //holds all file (image) names in the chosen directory
        ArrayList<String> myArr = new ArrayList<String>();

        //only allow directory selection
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //current directory is set
        chooser.setCurrentDirectory(new java.io.File("."));

        //pops up file chooser dialog, user chooses a directory
        int returnVal = chooser.showOpenDialog(null);

        //if the selected option was approved
        if(returnVal == JFileChooser.APPROVE_OPTION) 
        {
            //directory object
            File folder = chooser.getSelectedFile();
            //directory string
            directory = chooser.getSelectedFile() + "\\";
            //list files objects in the directory object
            listOfFiles = folder.listFiles();

            //put all the names of the file objects into myArr
            for (int i = 0; i < listOfFiles.length; i++) 
            {
                if (listOfFiles[i].isFile()) 
                {
                    myArr.add(directory+listOfFiles[i].getName());
                }//end inner if
            }//end for         
        }//end outer if
        //else no selection was made
        else 
        {
            System.out.println("No Selection ");
        }//end else

        return myArr;
    }//end method

comments on my code above: 上面的代码注释:

this code uses the JFileChooser to allow you to select a directory and return an ArrayList of Strings of all the image files in that folder. 此代码使用JFileChooser允许您选择目录并返回该文件夹中所有图像文件的字符串数组列表。 You can later use these Strings to make ImageIcons by iterating through the ArrayList one by one, and restrict to only 5 strings for 5 images. 以后,您可以使用这些字符串通过逐一遍历ArrayList来制作ImageIcons,并限制5个图像仅使用5个字符串。 If you have read my link provided above, creating ImageIcons should be a chinch. 如果您已阅读上面提供的我的链接,那么创建ImageIcons应该是个不错的选择。 Last step is to integrate all of the above with a java swing form- which would be obvious by now, trivial, really. 最后一步是将上述所有内容与Java swing表单集成在一起-到目前为止,这确实是微不足道的。 You can create a button to load more than 5 images. 您可以创建一个按钮来加载5张以上的图像。 Good luck! 祝好运!

I image a JFrame that contains a JScrollPane. 我镜像了一个包含JScrollPane的JFrame。 This JScrollPane contains a JPanel. 此JScrollPane包含一个JPanel。 The JPanel has a GridLayout for the number of pictures. JPanel具有用于图片数量的GridLayout。 Set the Viewport of the JSCrollPane to the JPanel. 将JSCrollPane的视口设置为JPanel。 Add the pictures to the JPanels. 将图片添加到JPanels。


JFrame theFrame = new JFrame();
JPanel picPanel = new JPanel();
JScrollPane scoller = new JScrollPane(picPanel);
theFrame.add(scroller);
//set layout to number of pics
picPanel.setLayout(new GridLayout(numOfPics,1));
//add pics to pic panel

You can use ImageIO to read in images from a directory 您可以使用ImageIO从目录中读取图像


BufferedImage newImage = ImageIO.read(theFile)

And then add it to a JLabel which can then be added to the JPanel 然后将其添加到JLabel,然后可以将其添加到JPanel


JLabel newImageLabel = new JLabel();
newImage.setIcon(new ImageIcon(newImage));
picPanel.add(newImageLabel);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM