这是用JAVA实现的仿Windows文件文件搜索程序。但是目前只是实现Windows里“所有文件夹和文件”相同的功能,在搜索过程中不单单是搜索文件,包含了搜索关键字的文件夹也会在搜索结果中显现出来。如果要做到更精确的搜索,还要进一步完善其中doSearch 类。不过还是觉得用JAVA来做界面,真的比较难做漂亮。我学习的重点也不在这里,总觉得即使是Swing也还是不够,sun应该还是拿出更好的界面编程类出来吧。
- /*
- *modelthefunctionofMSwindows"searchfile"byusingJava
- */
- importjava.awt.Component;
- importjava.awt.GridBagConstraints;
- importjava.awt.GridBagLayout;
- importjava.awt.event.ActionEvent;
- importjava.awt.event.ActionListener;
- importjava.io.File;
- importjavax.swing.JButton;
- importjavax.swing.JFileChooser;
- importjavax.swing.JFrame;
- importjavax.swing.JLabel;
- importjavax.swing.JPanel;
- importjavax.swing.JScrollPane;
- importjavax.swing.JTextArea;
- importjavax.swing.JTextField;
- /**
- *@authorSancho_lai
- *
- */
- publicclassUIfileSearchextendsJFrame{
- publicUIfileSearch(){
- this.setTitle("文件搜索JAVA实现");
- this.setSize(600,400);
- this.setLocationRelativeTo(null);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.add(newSearchFilePanel());
- }
- publicstaticvoidmain(String[]args){
- UIfileSearchtest=newUIfileSearch();
- System.out.println("5");
- test.setVisible(true);
- }
- }
- classSearchFilePanelextendsJPanel{
- /**
- *路径浏览和查找按钮
- *Buttonsforbrowsingthefilesystemandforstartingthesearch
- */
- privateJButtonbrowse,search;
- /**
- *查找条件,路径,查找过程
- *theconditions,directory,processofSearching,
- */
- privateJLabelfilter,directory,statusShow1,statusShow2;
- /**
- *输入查找条件和路径的文本框
- *TextFieldforinputingthesearchfilterandthefilepath
- */
- privateJTextFieldtextFilter,textDirectory;
- /**
- *显示查找结果的文本区
- *thetextAreatoshowtheresultofsearching
- */
- privateJTextArearesult;
- privateFileselectedFile;
- /**
- *找到的文件数量
- *numberoffilehadfound
- */
- privatelongcountFiles;
- /**
- *temporaryvariabletostorethe<filter>and<directory>
- */
- privateStringp,f;
- publicSearchFilePanel(){
- filter=newJLabel("AllorpartofFilename",2);
- directory=newJLabel("Lookin",2);
- statusShow1=newJLabel("readytosearch...",2);
- statusShow2=newJLabel("Thenumbersoffileshadbeenfound:");
- textFilter=newJTextField(15);
- textDirectory=newJTextField(15);
- result=newJTextArea();
- result.setEditable(false);
- JScrollPanejs=newJScrollPane(result);
- browse=newJButton("Browse");
- search=newJButton("StartSearch");
- /**
- *给浏览按钮设置监听事件
- *addActionListenerforbutton<t>browse</t>directly
- */
- browse.addActionListener(newActionListener(){
- publicvoidactionPerformed(ActionEventevt){
- JFileChooserjfc=newJFileChooser();
- //Justchooseadirectoryaddress
- jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- intd=jfc.showDialog(null,"Browsethefiles");
- if(d==JFileChooser.APPROVE_OPTION){
- selectedFile=jfc.getSelectedFile();
- textDirectory.setText(selectedFile.getPath());
- }
- }
- });
- /**
- *给“搜索”按钮增加监听事件
- *addActionListenerforbutton<t>Search</t>directly
- */
- search.addActionListener(newActionListener(){
- publicvoidactionPerformed(ActionEventevt){
- countFiles=0;
- result.setText("");
- f=textFilter.getText().trim();
- p=textDirectory.getText().trim();
- if("".equals(f)){
- System.out.println("filtercannotbenull,plesefillit");
- }if("".equals(p)){
- System.out.println("pathcannotbenull,pleasefillit!");
- }else{
- ThreadsearchT=newThread(newRunnable(){
- publicvoidrun(){
- doSearch(f,p);
- }
- });
- searchT.start();
- }
- }
- });
- setLayout(newGridBagLayout());
- GridBagConstraintsgc=newGridBagConstraints();
- gc.anchor=GridBagConstraints.WEST;
- addComponet(gc,filter,0,0,1,1);
- addComponet(gc,directory,0,1,1,1);
- gc.fill=GridBagConstraints.HORIZONTAL;
- gc.weightx=100;
- addComponet(gc,textFilter,1,0,1,1);
- addComponet(gc,textDirectory,1,1,1,1);
- gc.fill=GridBagConstraints.NONE;
- gc.weightx=0;
- addComponet(gc,search,2,0,1,1);
- addComponet(gc,browse,2,1,1,1);
- gc.fill=GridBagConstraints.HORIZONTAL;
- gc.weightx=100;
- addComponet(gc,statusShow1,0,2,3,1);
- addComponet(gc,statusShow2,0,3,3,1);
- gc.fill=GridBagConstraints.BOTH;
- gc.weighty=100;
- addComponet(gc,js,0,4,3,1);
- }
- publicvoidaddComponet(GridBagConstraintsgc,Componentc,intx,inty,
- intw,inth)
- {
- gc.gridx=x;
- gc.gridy=y;
- gc.gridwidth=w;
- gc.gridheight=h;
- add(c,gc);
- }
- /**
- *搜索监听事件的实现
- *implementtheActionLitener
- */
- privatevoiddoSearch(Stringfilter,Stringpath){
- Filefile=newFile(path);
- if(file.exists()){
- if(file.isDirectory()){
- File[]fileArray=file.listFiles();
- for(Filef:fileArray){
- if(f.isDirectory()){
- doSearch(filter,f.getPath());
- }else{
- if(f.getName().indexOf(filter)>=0){
- countFiles++;
- result.append(f.getPath()+"/r/n");
- }
- }
- statusShow1.setText(f.getPath());
- }
- statusShow2.setText("Thenumbersoffileshadbeenfound:"+countFiles);
- }else{
- System.out.println("Couldn'topenthepath!");
- }
- }else{
- System.out.println("ThepathhadbeenapointedwasnotExist!");
- }
- }
- }