Chinaunix首页 | 论坛 | 博客
  • 博客访问: 44281
  • 博文数量: 10
  • 博客积分: 253
  • 博客等级: 二等列兵
  • 技术积分: 131
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-09 08:08
文章分类
文章存档

2012年(10)

我的朋友

分类: Java

2012-04-09 08:16:44


点击(此处)折叠或打开

  1. package fileReseach;

  2. import java.awt.BorderLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.io.File;

  6. import javax.swing.DefaultListModel;
  7. import javax.swing.JButton;
  8. import javax.swing.JFrame;
  9. import javax.swing.JList;
  10. import javax.swing.JPanel;
  11. import javax.swing.JScrollPane;
  12. import javax.swing.JTextField;
  13. import javax.swing.ListSelectionModel;


  14. /**
  15.  *
  16.  *
  17.  * @author lee
  18.  *
  19.  */

  20. public class FileReseach extends JFrame implements ActionListener {

  21.     private JList fileList = null;
  22.     private DefaultListModel model = null;
  23.     private JScrollPane listScroller = null;
  24.     private JTextField pathInput = null;
  25.     private JPanel up = null;
  26.     private JButton search = null;

  27.     private String path = null;

  28.     public FileReseach() {

  29.         super();
  30.         this.setLayout(new BorderLayout());

  31.         up = new JPanel();
  32.         search = new JButton("Search !");
  33.         search.addActionListener(this);
  34.         pathInput = new JTextField(20);
  35.         up.add(pathInput);
  36.         up.add(search);

  37.         this.add(up, BorderLayout.NORTH);

  38.         //

  39.         model = new DefaultListModel();
  40.         fileList = new JList(model);

  41.         fileList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
  42.         fileList.setLayoutOrientation(JList.VERTICAL);
  43.         listScroller = new JScrollPane(fileList);

  44.         this.add(listScroller, BorderLayout.CENTER);
  45.         // this.pack();
  46.         this.setSize(400, 600);
  47.         //
  48.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  49.         this.setVisible(true);

  50.     }

  51.     public void actionPerformed(ActionEvent arg0) {

  52.         model.removeAllElements();
  53.         path = pathInput.getText();
  54.         displayAll(path, "");
  55.         // model.addElement("toto");
  56.     }

  57.     
  58.     /**
  59.      *
  60.      * @param path chemin du repertoire courant
  61.      * @param separator un tab
  62.      * @see afficher le contenu d'un repertoire recursivement
  63.      *
  64.      */
  65.     public void displayAll(String path, String separator) {

  66.         File dir = new File(path);

  67.         File tmp;

  68.         String[] list = dir.list();

  69.         try {
  70.             for (String name : list) {
  71.                 tmp = new File(path + "/" + name);
  72.                 if (tmp.isFile() == true) {
  73.                     String str = tmp.getAbsolutePath();
  74.                     String sep = separator;
  75.                     //System.out.println(sep + str);
  76.                     model.addElement(str+sep);
  77.                     // System.out.println(separator + "->" +str);
  78.                 }

  79.                 else {
  80.                     if (tmp.canExecute()) {
  81.                         System.out.println(separator + name);
  82.                         displayAll(path + "/" + name, "    " + separator);
  83.                     }
  84.                 }
  85.             }
  86.         } catch (Exception e) {
  87.             e.printStackTrace();
  88.         }

  89.     }

  90.     public static void main(String[] argv) {
  91.         FileReseach fr = new FileReseach();
  92.     }
  93.     


  94. }

阅读(652) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~