如何在JavaSwing表单中添加来自目录的数字图像?

如何在JavaSwing表单中添加来自目录的数字图像?,java,swing,Java,Swing,对于我的项目,我需要以JavaSwing形式显示文件目录中的许多图像。每次执行时,目录、图像数量及其路径都会发生变化。我计划在表单中显示固定数量的图像,比如5个,并提供一个按钮在同一位置加载后续图像。图像文件名是连续的(如eg1.jpg、eg2.jpg、eg3.jpg…) 我使用的是Netbeans 6.9.1 IDE 解析目录中的文件列表 循环浏览列表,最多显示前五个 如果存在5个以上的图像,请添加一个按钮 有几种方法可以将图像添加到Swing中的容器中。最简单的方法之一是将其作为图标添加到J

对于我的项目,我需要以JavaSwing形式显示文件目录中的许多图像。每次执行时,目录、图像数量及其路径都会发生变化。我计划在表单中显示固定数量的图像,比如5个,并提供一个按钮在同一位置加载后续图像。图像文件名是连续的(如eg1.jpg、eg2.jpg、eg3.jpg…) 我使用的是Netbeans 6.9.1 IDE

  • 解析目录中的文件列表
  • 循环浏览列表,最多显示前五个
  • 如果存在5个以上的图像,请添加一个按钮

  • 有几种方法可以将图像添加到Swing中的容器中。最简单的方法之一是将其作为图标添加到
    JLabel
    。我强烈建议手工操作,而不是使用GUI工具。

    我对包含JScrollPane的JFrame进行了镜像。此JScrollPane包含一个JPanel。JPanel有一个用于显示图片数量的网格布局。将JSCrollPane的视口设置为JPanel。将图片添加到JPanel

    
    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
    然后将其添加到JLabel,然后可以将其添加到JPanel

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

    我有一个解决办法给你。我在一个类似的项目中工作,发现link对显示图像非常有帮助。下载、运行并理解“IconDemo”项目。请注意,此项目中有两个独立的线程—加载图像的SwingWorker(因为这是一项资源密集型任务)和与GUI相关的EventDispatchThread

    至于从目录加载图片,您可以将上述项目与JFileChooser集成。下面是我的代码:

        //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
    
    //返回所选目录中的文件(图像)名称
    公共ArrayList getFileNames()
    {
    //让用户选择目录或文件的小部件
    JFileChooser chooser=新的JFileChooser();
    //保存所选目录中的所有文件(图像)名称
    ArrayList myArr=新的ArrayList();
    //仅允许目录选择
    setFileSelectionMode(仅限于JFileChooser.DIRECTORIES_);
    //已设置当前目录
    chooser.setCurrentDirectory(新的java.io.File(“.”);
    //弹出文件选择器对话框,用户选择一个目录
    int returnVal=chooser.showOpenDialog(null);
    //如果所选选项已批准
    if(returnVal==JFileChooser.APPROVE_选项)
    {
    //目录对象
    File folder=chooser.getSelectedFile();
    //目录字符串
    directory=chooser.getSelectedFile()+“\\”;
    //列出目录对象中的文件对象
    listOfFiles=folder.listFiles();
    //将文件对象的所有名称放入myArr
    for(int i=0;i
    对以上代码的评论:


    这段代码使用JFileChooser来允许您选择一个目录,并返回该文件夹中所有图像文件字符串的ArrayList。稍后,您可以通过逐个迭代ArrayList来使用这些字符串生成ImageIcon,并将5个图像限制为仅5个字符串。如果您已经阅读了我上面提供的链接,那么创建图像图标应该是一个难题。最后一步是将上述所有内容与java swing表单集成——这一点现在已经很明显了,非常简单。您可以创建一个按钮来加载5个以上的图像。祝你好运

    我同意手工做这件事
        //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