• 文件简例


    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    import javax.swing.event.ChangeEvent;

    public class list {
    public JList fileList;
    public static String[] bmp =new String[20];
    public static String[] gif =new String[20];
    public static String[] jpg =new String[20];
    public static int i=0,j=0,k=0;
    /**
    * @param args
    */
    public static void listenDirectory(File dir) throws IllegalAccessException{
    //判断dir是否存在,使用Fire类的
    if(!dir.exists()){
    //如果不存在,则抛出异常
    throw new IllegalAccessException("目录"+dir+"不存在。");
    }
    //判断dir是否为目录使用Fire类的isdirectory方法
    if(!dir.isDirectory()){
    throw new IllegalArgumentException(dir+"不是目录");
    }

        final File[] files = dir.listFiles();
        for(File file:files){
            if(file.getName().contains("bmp")){    //寻找文件名带有“bmp”的文件
            bmp[i]=dir+"\"+file.getName();        //将带有“bmp”的文件名放入数组中
            i++;}
            if(file.getName().contains("gif")){     //寻找文件名带有“gif”的文件
                gif[j]=dir+"\"+file.getName();     //将带有“gif”的文件名放入数组中
                j++;}
            if(file.getName().contains("jpg")){     //寻找文件名带有“jpg”的文件
                jpg[k]=dir+"\"+file.getName();     //将带有“jpg”的文件名放入数组中
                k++;}
    

    // System.out.println(file);
    }

        final JFrame frame = new JFrame();
        frame.getContentPane();
        frame.setLayout(new BorderLayout(2,1));
         String[] formats = { "所有格式", "bmp格式", "gif格式", "jpg格式" };
     final JComboBox formatList = new JComboBox(formats);
    final JList fileList = new JList(files);
       fileList.setForeground(new Color(60,8,6));            
       fileList.setBackground(new Color(0,0,0));                 
       fileList.setSelectionBackground(new Color(80,49,134));     
       fileList.setSelectionForeground(new Color(140,71,26));  
       final JScrollPane scrollPane = new JScrollPane(fileList);
       scrollPane.setColumnHeaderView(new JLabel("图片目录"));      
       frame.add(formatList, BorderLayout.NORTH);
       frame.add(scrollPane, BorderLayout.CENTER);
       frame.setVisible(true);
       Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
       frame.setBounds(d.width/2, d.height/2, 250, 400);
       //
       formatList.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e)
             {   if (e.getSource() == formatList)
             {  if(formatList.getSelectedIndex()==0)
             {
             fileList.setListData(files);       //列表框选择所有文件时,显示所有文件
             }
                 if(formatList.getSelectedIndex()==1)
             {
             fileList.setListData(bmp);         //列表框选择“bmp”文件时,显示带“bmp”的文件
             }
                
             if(formatList.getSelectedIndex()==2)
             {
             fileList.setListData(gif);          //列表框选择“gif”文件时,显示带“gif”的文件
             }
             if(formatList.getSelectedIndex()==3)
             {
             fileList.setListData(jpg);           //列表框选择“jpg”文件时,显示带“jpg”的文件
             }
                 }}});
             }
    public static void main(String[] args) {
        try {
            list.listenDirectory(new File("E:\"));
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }}
    

  • 相关阅读:
    通过url在两个页面之间传值
    $.ajax数据传输成功却执行失败的回调函数
    5.26小测
    洛谷——AC记
    7.2模拟赛
    6.30模拟赛
    洛谷——每日一题
    洛谷——动态规划
    致创营
    BSGS
  • 原文地址:https://www.cnblogs.com/CUI2014/p/5401312.html
Copyright © 2020-2023  润新知