显示默认目录中的所有文件名
2016-04-15 21:28
337 查看
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(); } }
![](https://images2015.cnblogs.com/blog/906029/201604/906029-20160420105225273-864756740.png)
相关文章推荐
- java import、package作用与用法
- MySQL数据类型大全
- c++下为使用pimpl方法的类编写高效的swap函数
- CF 271 Beautiful Year
- HDU 5256 序列变换(最长上升子序列)
- POJ 2387 Til the Cows Come Home
- 字符混编
- 笔试题26. LeetCode OJ (13)
- 【NOIP practice】 BSOJ1295 洗牌 智力题
- VijosP1100:加分二叉树
- Java设计模式--------建造者模式
- php获取文件后缀名
- PHP中try{}catch{}的具体用法详解
- Android:布局实例之模仿京东登录界面
- NOI2001《炮兵阵地》 状压DP
- 网易游戏笔试题:编写string类的构造函数、拷贝构造函数、赋值构造函数和析构函数
- HDU 5157 Harry and magic string
- 学习Android第8天,Asynctask的使用(异步操作)
- JavaEE包括13中核心技术
- table,tablespace监控