仿Windows文件搜索 JAVA 实现

这是用JAVA实现的仿Windows文件文件搜索程序。但是目前只是实现Windows里“所有文件夹和文件”相同的功能,在搜索过程中不单单是搜索文件,包含了搜索关键字的文件夹也会在搜索结果中显现出来。如果要做到更精确的搜索,还要进一步完善其中doSearch 类。不过还是觉得用JAVA来做界面,真的比较难做漂亮。我学习的重点也不在这里,总觉得即使是Swing也还是不够,sun应该还是拿出更好的界面编程类出来吧。
  1. /*
  2.  *model the function of MS windows "search file" by using Java 
  3.  */
  4. import java.awt.Component;
  5. import java.awt.GridBagConstraints;
  6. import java.awt.GridBagLayout;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.io.File;
  10. import javax.swing.JButton;
  11. import javax.swing.JFileChooser;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JPanel;
  15. import javax.swing.JScrollPane;
  16. import javax.swing.JTextArea;
  17. import javax.swing.JTextField;
  18. /**
  19.  * @author Sancho_lai
  20.  *
  21.  */
  22. public class UIfileSearch extends JFrame {
  23.     
  24.     public UIfileSearch() {
  25.         this.setTitle("文件搜索JAVA实现");
  26.         this.setSize(600,400);
  27.         this.setLocationRelativeTo(null);
  28.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29.         this.add(new SearchFilePanel());
  30.     }
  31.     
  32.     public static void main(String[] args) {
  33.         UIfileSearch test = new UIfileSearch();
  34.     System.out.println("5");
  35.         test.setVisible(true);
  36.     }
  37. }
  38. class SearchFilePanel extends JPanel{
  39.     
  40.     /**
  41.      * 路径浏览和查找按钮 
  42.      * Buttons for browsing the file system and for starting the search
  43.      */
  44.     private JButton browse,search;
  45.     /**
  46.      * 查找条件,路径,查找过程
  47.      * the conditions,directory,process of Searching,
  48.      */
  49.     private JLabel filter, directory,statusShow1,statusShow2;
  50.     /**
  51.      * 输入查找条件和路径的文本框
  52.      * TextField for inputing the search filter and the file path
  53.      */
  54.     private JTextField textFilter,textDirectory;
  55.     /**
  56.      * 显示查找结果的文本区
  57.      * the textArea to show the result of searching
  58.      */
  59.     private JTextArea result;
  60.     
  61.     
  62.     private File selectedFile;
  63.     /**
  64.      * 找到的文件数量
  65.      * number of file had found
  66.      */
  67.     private long countFiles;
  68.     /**
  69.      * temporary variable to store the <filter> and <directory>
  70.      */
  71.     private String p ,f;
  72.     
  73.     public  SearchFilePanel() {
  74.         
  75.         filter = new JLabel("All or part of File name",2);
  76.         directory = new JLabel("Look in",2);
  77.         statusShow1 = new JLabel("ready to search...",2);
  78.         statusShow2 = new JLabel("The numbers of files had been found:");
  79.         textFilter = new JTextField(15);
  80.         textDirectory = new JTextField(15);
  81.         result = new JTextArea();
  82.         result.setEditable(false);
  83.         JScrollPane js = new JScrollPane(result);
  84.         
  85.         browse = new JButton("Browse");
  86.         search = new JButton("Start Search");
  87.         /**
  88.          * 给浏览按钮设置监听事件
  89.          * add ActionListener for button <t>browse</t> directly
  90.          */
  91.         browse.addActionListener(new ActionListener(){
  92.             public void actionPerformed(ActionEvent evt) {
  93.                 JFileChooser jfc = new JFileChooser();
  94.                 //Just choose a directory address
  95.                 jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  96.                 int d = jfc.showDialog(null"Browse the files");
  97.                 if(d == JFileChooser.APPROVE_OPTION) {
  98.                     selectedFile = jfc.getSelectedFile();
  99.                     textDirectory.setText(selectedFile.getPath());
  100.                 }
  101.             }
  102.             
  103.         });
  104.         /**
  105.          * 给“搜索”按钮增加监听事件
  106.          * add ActionListener for button<t>Search</t>  directly
  107.          */
  108.         search.addActionListener(new ActionListener(){
  109.             public void actionPerformed(ActionEvent evt) {
  110.     
  111.                 countFiles = 0;
  112.                 result.setText("");
  113.     
  114.                 f = textFilter.getText().trim();
  115.                 p = textDirectory.getText().trim();
  116.                 
  117.                 if("".equals(f)) {
  118.                     System.out.println("filter can not be null,plese fill it");
  119.                 }if("".equals(p)) {
  120.                     System.out.println("path can not be null,please fill it!");
  121.                 }else {
  122.                     Thread searchT = new Thread(new Runnable(){
  123.                         public void run() {
  124.                             doSearch(f,p);
  125.                         }
  126.                     });
  127.                     searchT.start();
  128.                 }
  129.             }
  130.             
  131.         });
  132.         
  133.        
  134.         setLayout(new GridBagLayout());
  135.         GridBagConstraints gc = new GridBagConstraints(); 
  136.         gc.anchor = GridBagConstraints.WEST;
  137.         addComponet(gc, filter, 0011);
  138.         addComponet(gc, directory, 0111); 
  139.    
  140.         gc.fill = GridBagConstraints.HORIZONTAL;
  141.         gc.weightx = 100;
  142.         addComponet(gc, textFilter, 1011);
  143.         addComponet(gc, textDirectory, 1111); 
  144.         gc.fill = GridBagConstraints.NONE;
  145.         gc.weightx = 0;
  146.         addComponet(gc, search, 2011);
  147.         addComponet(gc, browse, 2111); 
  148.    
  149.         gc.fill = GridBagConstraints.HORIZONTAL;
  150.         gc.weightx = 100;
  151.         addComponet(gc, statusShow1, 0231); 
  152.         addComponet(gc, statusShow2, 0331); 
  153.         
  154.         gc.fill = GridBagConstraints.BOTH;
  155.         gc.weighty = 100;
  156.         addComponet(gc, js, 0431);
  157.     }
  158.      public void addComponet(GridBagConstraints gc, Component c, int x, int y,
  159.        int w, int h)
  160.      {
  161.       gc.gridx = x;
  162.       gc.gridy = y;
  163.       gc.gridwidth = w;
  164.       gc.gridheight = h;
  165.       add(c, gc);
  166.      } 
  167.     /**
  168.      * 搜索监听事件的实现
  169.      * implement the ActionLitener
  170.      */
  171.     private void doSearch(String filter, String path) {
  172.         File file = new File(path);
  173.         if(file.exists()) {
  174.             if(file.isDirectory()) {
  175.                 File[] fileArray = file.listFiles();
  176.                 for(File f:fileArray) {
  177.                     if(f.isDirectory()) {
  178.                         doSearch(filter,f.getPath());
  179.                     } else {
  180.                         if(f.getName().indexOf(filter) >= 0) {
  181.                             countFiles++;
  182.                             result.append(f.getPath() + "/r/n");
  183.                         }
  184.                     }
  185.                     statusShow1.setText(f.getPath());
  186.                 }
  187.                 statusShow2.setText("The numbers of files had been found:" + countFiles);
  188.             } else {
  189.                 System.out.println("Couldn't open the path!");
  190.             }
  191.         } else {
  192.             System.out.println("The path had been apointed was not Exist!");
  193.         }
  194.     }
  195. }

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值