zoukankan      html  css  js  c++  java
  • 显示默认目录中的所有文件名

    import java.awt.BorderLayout;..

      import javax.swing.JScrollPane;
    
     public class FileList extends JFrame{
    private static final String FILES_DIR = "audio";
    private JList fileList;	
    
    public FileList(){
    	super("FileList");
    	String[] fileNames = findFiles(FILES_DIR, null);
        makeFrame(fileNames);
    	// TODO Auto-generated constructor stub
    }
    
    private void makeFrame(String[] autoNames) {
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
    	JPanel contentPane = (JPanel)getContentPane();
    	fileList = new JList(autoNames);
        fileList.setForeground(new Color(140,171,226));
        fileList.setBackground(new Color(0,0,0));
        fileList.setSelectionBackground(new Color(87,49,134));
        fileList.setSelectionForeground(new Color(140,171,226));
        JScrollPane scrollPane = new JScrollPane(fileList);        
        scrollPane.setColumnHeaderView(new JLabel("files list"));
        contentPane.add(scrollPane, BorderLayout.CENTER);
        
        pack();
        // place this frame at the center of the screen and show
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(d.width/2 - getWidth()/2, d.height/2 - getHeight()/2);
        setVisible(true);
    }
    
    private String[] findFiles(String dirName, String suffix) {
    	File dir = new File(dirName);
        if(dir.isDirectory()) {
            String[] allFiles = dir.list();
            if(suffix == null) {
                return allFiles;
            }
            else {
                List<String> selected = new ArrayList<String>();
                for(String filename : allFiles) {
                    if(filename.endsWith(suffix)) {
                        selected.add(filename);
                    }
                }
                return selected.toArray(new String[selected.size()]);
            }
        }
        else {
            System.out.println("Error: " + dirName + " must be a directory");
            return null;
         }
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
    	new FileList();
     }
    
       }
    

  • 相关阅读:
    jquery插件开发
    五种常见的 PHP 设计模式
    linux常用命令
    解决MySQL不允许从远程访问的方法
    模块化的JavaScript开发的优势在哪里
    巧用C#做中间语言 实现Java调用.net DLL
    PHP Predefined Interfaces 预定义接口
    想追赶.Net的脚步?Java面前障碍重重
    修改一行SQL代码 性能提升了N倍
    如何使用LoadRunner监控Windows
  • 原文地址:https://www.cnblogs.com/yangbinge/p/5396998.html
Copyright © 2011-2022 走看看