Java桌面搜索程序部分说明

1.文件后缀搜索用递归方法。
   递归是:笼统的说是对同一个方法的不断调用。

 1 private   void  LookFor(File dir,String yt)  {
 2         for(String nm : dir.list()){
 3            File wjhz = new File(dir.getAbsolutePath() + "/" + nm);
 4            if(wjhz.isFile()){
 5                int counter=0;
 6                int error=0;
 7                 String ext = nm.substring(nm.lastIndexOf("."));
 8                 if(yt.equals(ext)){
 9                     counter++;
10                     String first = jEditorPane1.getText();
11                     jEditorPane1.setText(first+"\n"+ counter + wjhz.getName()) ;
12                     String two = jTextPane2.getText();
13                     jTextPane2.setText(two +"\n"+wjhz.getPath());
14                     
15                 }
else {
16                     error++;
17                 }

18            }
else{
19                LookFor(wjhz,yt);
20            }

21        }

22    }


2.在GUI中进行超链接设置到文件。界面如图
  

















用到的代码如下:

 

 1  import  java.awt.BorderLayout;
 2  import  java.io.File;
 3  import  java.io.IOException;
 4  import  javax.swing.JEditorPane;
 5  import  javax.swing.JFileChooser;
 6  import  javax.swing.JFrame;
 7  import  javax.swing.JScrollPane;
 8  import  javax.swing.SwingUtilities;
 9  import  javax.swing.event.HyperlinkEvent;
10  import  javax.swing.event.HyperlinkListener;
11 
12  public   class  EditorPaneSample {
13           public   static   void  main(String args[])  throws  IOException {
14                  JFrame f  =   new  JFrame( " JEditorPane Sample " );
15                  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
16 
17                  JEditorPane editor  =   new  JEditorPane( " text/html " , " <center><a href=\ " \ " >浏览文件<center> " );
18                  editor.setEditable( false );
19 
20                  HyperlinkListener hyperlinkListener  =   new  ActivatedHyperlinkListener(f,
21                                  editor);
22                  editor.addHyperlinkListener(hyperlinkListener);
23 
24                  JScrollPane scrollPane  =   new  JScrollPane(editor);
25                  f.getContentPane().add(scrollPane, BorderLayout.CENTER);
26                  f.setSize( 300 200 );
27                  f.setVisible( true );
28          }
29  }
30 
31  class  ActivatedHyperlinkListener  implements  HyperlinkListener {
32 
33          JFrame frame;
34          JEditorPane editorPane;
35 
36           public  ActivatedHyperlinkListener(JFrame frame, JEditorPane editorPane) {
37                   this .frame  =  frame;
38                   this .editorPane  =  editorPane;
39          }
40 
41           public   void  hyperlinkUpdate(HyperlinkEvent hyperlinkEvent) {
42                  HyperlinkEvent.EventType type  =  hyperlinkEvent.getEventType();
43 
44                   if  (type  ==  HyperlinkEvent.EventType.ACTIVATED) {
45                          Runnable runner  =   new  Runnable() {
46                                   public   void  run() {
47                                          JFileChooser chooser  =   new  JFileChooser( " . " );
48                                          chooser.setAcceptAllFileFilterUsed( false );
49 
50                                           int  status  =  chooser.showOpenDialog(editorPane);
51                                           if  (status  ==  JFileChooser.APPROVE_OPTION) {
52                                                  File f  =  chooser.getSelectedFile();
53                                                  System.out.println(f);
54                                          }
55                                  }
56                          };
57                          SwingUtilities.invokeLater(runner);
58                  }
59          }
60  }



  

3.双击一个按钮后能够打开一个制定的txt文件。
    技术代码如下:
   
1 if (evt.getClickCount()   ==    2 ) {
2            try{
3                String getpath = "notepad.exe"+" "+jTextField1.getText();
4                Runtime.getRuntime().exec(getpath);
5            }
catch(Exception   e){
6                System.out.println(e.getMessage());
7            }

8        }
4.你如果有什么好的方法请不要忘记留言呀!你也可以发到[email protected]

你可能感兴趣的:(Java桌面搜索程序部分说明)