1 import javax.swing.*;
2 import java.awt.*;
3 import java.io.File;
4
5 public class FileUtil extends JFrame{
6
7 /**
8 * @param args
9 */
10
11 public static String listDirectory(File dir) throws IllegalAccessException{
12 if(!dir.exists()){
13 throw new IllegalAccessException("目录"+dir+"不存在");
14 }
15 if(!dir.isDirectory()){//判断是不是目录
16 throw new IllegalArgumentException(dir+"不是目录");
17 }
18 String[] fileName = dir.list();
19 String name ="";
20 for(String a : fileName){
21 name=name+a+"\n";}
22 return name;
23 }
24
25 public static void main(String[] args) {
26 // TODO Auto-generated method stub
27 JFrame frame=new JFrame();
28 JPanel main_panel =new JPanel(new BorderLayout());
29 JLabel label = new JLabel("FileList");
30 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
31 //frame.setLayout(null);
32 String[] itme =new String[]{"文件夹A","文件夹B"};
33 JComboBox box=new JComboBox();
34 for(int i=0;i<itme.length;i++){
35 box.addItem(itme[i]);
36 }
37 box.setEnabled(true);
38 box.setEditable(true);
39 box.setMaximumRowCount(6);
40 box.setBounds(230,30,130,25);
41
42 frame.setBounds(400,300,400,200);
43 frame.setVisible(true);
44 JTextArea main_text =new JTextArea();
45 main_text.setBackground(Color.red);
46 JScrollPane AA=new JScrollPane();
47 AA.setViewportView(main_text);
48 main_text.setEnabled(false);
49 main_panel.add(box,BorderLayout.NORTH);
50 main_panel.add(AA,BorderLayout.CENTER);
51 main_panel.add(label,BorderLayout.SOUTH);
52 frame.add(main_panel);
53
54 try {
55 String str=FileUtil.listDirectory(new File("C:\\Users\\woshinibaba\\Desktop\\Java作业5\\soundPlayer"));
56 main_text.setText(str);
57 }
58 catch (IllegalAccessException e) {
59 // TODO Auto-generated catch block
60 e.printStackTrace();
61 }
62
63
64
65 frame.setBounds(400,200,200,200);
66 frame.setVisible(true);
67
68
69 }
70 }
![](https://images2015.cnblogs.com/blog/903620/201604/903620-20160417164526457-641226062.png)
![](https://images2015.cnblogs.com/blog/903620/201604/903620-20160417164542738-1372026384.png)