仿Windows文件搜索 JAVA 实现

这是用JAVA实现的仿Windows文件文件搜索程序。但是目前只是实现Windows里“所有文件夹和文件”相同的功能,在搜索过程中不单单是搜索文件,包含了搜索关键字的文件夹也会在搜索结果中显现出来。如果要做到更精确的搜索,还要进一步完善其中doSearch 类。不过还是觉得用JAVA来做界面,真的比较难做漂亮。我学习的重点也不在这里,总觉得即使是Swing也还是不够,sun应该还是拿出更好的界面编程类出来吧。
  1. /*
  2. *modelthefunctionofMSwindows"searchfile"byusingJava
  3. */
  4. importjava.awt.Component;
  5. importjava.awt.GridBagConstraints;
  6. importjava.awt.GridBagLayout;
  7. importjava.awt.event.ActionEvent;
  8. importjava.awt.event.ActionListener;
  9. importjava.io.File;
  10. importjavax.swing.JButton;
  11. importjavax.swing.JFileChooser;
  12. importjavax.swing.JFrame;
  13. importjavax.swing.JLabel;
  14. importjavax.swing.JPanel;
  15. importjavax.swing.JScrollPane;
  16. importjavax.swing.JTextArea;
  17. importjavax.swing.JTextField;
  18. /**
  19. *@authorSancho_lai
  20. *
  21. */
  22. publicclassUIfileSearchextendsJFrame{
  23. publicUIfileSearch(){
  24. this.setTitle("文件搜索JAVA实现");
  25. this.setSize(600,400);
  26. this.setLocationRelativeTo(null);
  27. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28. this.add(newSearchFilePanel());
  29. }
  30. publicstaticvoidmain(String[]args){
  31. UIfileSearchtest=newUIfileSearch();
  32. System.out.println("5");
  33. test.setVisible(true);
  34. }
  35. }
  36. classSearchFilePanelextendsJPanel{
  37. /**
  38. *路径浏览和查找按钮
  39. *Buttonsforbrowsingthefilesystemandforstartingthesearch
  40. */
  41. privateJButtonbrowse,search;
  42. /**
  43. *查找条件,路径,查找过程
  44. *theconditions,directory,processofSearching,
  45. */
  46. privateJLabelfilter,directory,statusShow1,statusShow2;
  47. /**
  48. *输入查找条件和路径的文本框
  49. *TextFieldforinputingthesearchfilterandthefilepath
  50. */
  51. privateJTextFieldtextFilter,textDirectory;
  52. /**
  53. *显示查找结果的文本区
  54. *thetextAreatoshowtheresultofsearching
  55. */
  56. privateJTextArearesult;
  57. privateFileselectedFile;
  58. /**
  59. *找到的文件数量
  60. *numberoffilehadfound
  61. */
  62. privatelongcountFiles;
  63. /**
  64. *temporaryvariabletostorethe<filter>and<directory>
  65. */
  66. privateStringp,f;
  67. publicSearchFilePanel(){
  68. filter=newJLabel("AllorpartofFilename",2);
  69. directory=newJLabel("Lookin",2);
  70. statusShow1=newJLabel("readytosearch...",2);
  71. statusShow2=newJLabel("Thenumbersoffileshadbeenfound:");
  72. textFilter=newJTextField(15);
  73. textDirectory=newJTextField(15);
  74. result=newJTextArea();
  75. result.setEditable(false);
  76. JScrollPanejs=newJScrollPane(result);
  77. browse=newJButton("Browse");
  78. search=newJButton("StartSearch");
  79. /**
  80. *给浏览按钮设置监听事件
  81. *addActionListenerforbutton<t>browse</t>directly
  82. */
  83. browse.addActionListener(newActionListener(){
  84. publicvoidactionPerformed(ActionEventevt){
  85. JFileChooserjfc=newJFileChooser();
  86. //Justchooseadirectoryaddress
  87. jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  88. intd=jfc.showDialog(null,"Browsethefiles");
  89. if(d==JFileChooser.APPROVE_OPTION){
  90. selectedFile=jfc.getSelectedFile();
  91. textDirectory.setText(selectedFile.getPath());
  92. }
  93. }
  94. });
  95. /**
  96. *给“搜索”按钮增加监听事件
  97. *addActionListenerforbutton<t>Search</t>directly
  98. */
  99. search.addActionListener(newActionListener(){
  100. publicvoidactionPerformed(ActionEventevt){
  101. countFiles=0;
  102. result.setText("");
  103. f=textFilter.getText().trim();
  104. p=textDirectory.getText().trim();
  105. if("".equals(f)){
  106. System.out.println("filtercannotbenull,plesefillit");
  107. }if("".equals(p)){
  108. System.out.println("pathcannotbenull,pleasefillit!");
  109. }else{
  110. ThreadsearchT=newThread(newRunnable(){
  111. publicvoidrun(){
  112. doSearch(f,p);
  113. }
  114. });
  115. searchT.start();
  116. }
  117. }
  118. });
  119. setLayout(newGridBagLayout());
  120. GridBagConstraintsgc=newGridBagConstraints();
  121. gc.anchor=GridBagConstraints.WEST;
  122. addComponet(gc,filter,0,0,1,1);
  123. addComponet(gc,directory,0,1,1,1);
  124. gc.fill=GridBagConstraints.HORIZONTAL;
  125. gc.weightx=100;
  126. addComponet(gc,textFilter,1,0,1,1);
  127. addComponet(gc,textDirectory,1,1,1,1);
  128. gc.fill=GridBagConstraints.NONE;
  129. gc.weightx=0;
  130. addComponet(gc,search,2,0,1,1);
  131. addComponet(gc,browse,2,1,1,1);
  132. gc.fill=GridBagConstraints.HORIZONTAL;
  133. gc.weightx=100;
  134. addComponet(gc,statusShow1,0,2,3,1);
  135. addComponet(gc,statusShow2,0,3,3,1);
  136. gc.fill=GridBagConstraints.BOTH;
  137. gc.weighty=100;
  138. addComponet(gc,js,0,4,3,1);
  139. }
  140. publicvoidaddComponet(GridBagConstraintsgc,Componentc,intx,inty,
  141. intw,inth)
  142. {
  143. gc.gridx=x;
  144. gc.gridy=y;
  145. gc.gridwidth=w;
  146. gc.gridheight=h;
  147. add(c,gc);
  148. }
  149. /**
  150. *搜索监听事件的实现
  151. *implementtheActionLitener
  152. */
  153. privatevoiddoSearch(Stringfilter,Stringpath){
  154. Filefile=newFile(path);
  155. if(file.exists()){
  156. if(file.isDirectory()){
  157. File[]fileArray=file.listFiles();
  158. for(Filef:fileArray){
  159. if(f.isDirectory()){
  160. doSearch(filter,f.getPath());
  161. }else{
  162. if(f.getName().indexOf(filter)>=0){
  163. countFiles++;
  164. result.append(f.getPath()+"/r/n");
  165. }
  166. }
  167. statusShow1.setText(f.getPath());
  168. }
  169. statusShow2.setText("Thenumbersoffileshadbeenfound:"+countFiles);
  170. }else{
  171. System.out.println("Couldn'topenthepath!");
  172. }
  173. }else{
  174. System.out.println("ThepathhadbeenapointedwasnotExist!");
  175. }
  176. }
  177. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值