⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selectkey.java

📁 在指定的包含源码或其他问本的目录下搜索待查找关键字:包括模糊查找
💻 JAVA
字号:
import java.lang.*;
import java.awt.*;
import java.awt.List;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.util.*;

public class selectKey extends Frame
{
  selectKey Form1;
  boolean selectState = false;
       
  Label L1 = new Label("欲查询的目录:");
  TextField Text1 = new TextField();      
  Label L2 = new Label("完全比较(只能含英文、数字、下划线):");
  TextField Text2 = new TextField("Reader");   
  Button B1=new Button("查询");   
  Label L3 = new Label("模糊比较(不可含空白):");
  TextField Text3 = new TextField("Reader");     
  Button B2=new Button("查询");
  Label L4 = new Label();
  List List1 = new List();    
  public selectKey()
  {
    Form1 = this;
    this.setLayout( null ); 
    this.setTitle("搜索程序源码内容");
    this.setBounds(300,100,600,400); 
    this.setBackground( Color.PINK );
    
    L1.setBounds(15,30,205,25);
    L1.setBackground( new Color(255,255,150) );      
    this.add(L1);
    Text1.setBounds(220,30,220,25);	
    this.add(Text1);        
       
    L2.setBounds(15,70,205,25);
    L2.setBackground( new Color(255,255,150) );      
    this.add(L2);
    Text2.setBounds(220,70,220,25);	
    this.add(Text2);      
    B1.setBounds(450,70,80,25);
    B1.addActionListener(new myAction());	
    this.add(B1);
    
    L3.setBounds(15,110,205,25);
    L3.setBackground( new Color(255,255,150) );    
    this.add(L3);
    Text3.setBounds(220,110,220,25);	
    this.add(Text3);         
    B2.setBounds(450,110,80,25);
    B2.addActionListener(new myAction());	
    this.add(B2);
    
    L4.setBounds(15,150,this.getWidth()-30,25);
    L4.setBackground( new Color(255,255,150) ); 
    this.add(L4);      
    List1.setBounds(15,190,this.getWidth()-30,this.getHeight()-210);
    List1.addActionListener(new myAction());	
    this.add(List1);          
    
    this.addWindowListener( new WindowAdapter()
    { 
      public void windowClosing(WindowEvent e)
      {
        System.exit( 0 );   //结束系统
      }            
    });
    this.setVisible( true ); 
    
    try
    {
      String SelectDir = (new File("./")).getCanonicalPath();
      Text1.setText(SelectDir); //显示此 selectKey.class 文件所在目录
     }
     catch(IOException ecp){}        
  }
  public void paint(Graphics g)
  {
    L4.setBounds(15,150,this.getWidth()-30,25);
    List1.setBounds(15,190,this.getWidth()-30,this.getHeight()-210);
  }

  class select1 extends Thread
  { 
    public void run()
    {
      File tar = new File( Text1.getText().trim() );
      Form1.List1.removeAll();           
      Form1.SelectFiles(tar);
      selectState = false ;
      JOptionPane.showMessageDialog(Form1,"找完了!");	          
    } 
  }
  class select2 extends Thread   
  { 
    public void run()
    {
      File tar = new File( Text1.getText().trim() );
      Form1.List1.removeAll();           
      Form1.SelectFiles2(tar);
      selectState = false;
      JOptionPane.showMessageDialog(Form1,"找完了!");	        
    } 
  }
  class showSelectedFile extends Thread
  { 
    public void run()
    {
      String selected = List1.getSelectedItem();
      String FileName = selected.substring(selected.indexOf(' ')+1);
      new JavaFile( FileName );       
    } 
  }  
   
  class myAction implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
      if(e.getSource()==B1)
      {
        if( selectState == false )
        {   //目前沒在进行搜索工作
          selectState = true;
          (new select1()).start();
        }
      }
      else if(e.getSource()==B2)
      {
        if( selectState == false )
        {   
          selectState = true;
          (new select2()).start();		       
        }
      } 
      else if(e.getSource()==List1)
      {
        (new showSelectedFile()).start();
      }		   
    }
  } //class Button1Mouse1 end    

  public void SelectFiles(File tarDir)
  {         
    if(tarDir.exists())
    {
      File[] FileList = tarDir.listFiles();
      if( FileList!=null && FileList.length>0)
      {	   //此 File 代表目录,且其内有文件
        String tmpLine = null;
        String tmpStr = null;
        for(int x=0; x<FileList.length; x++)
        {
          if(FileList[x].isDirectory())
             SelectFiles(FileList[x]);	//递归
          else if( FileList[x].getName().endsWith(".java") )
          {
            try
            {
              L4.setText( FileList[x].getCanonicalPath() );
              /*========== 使用 StreamTokenizer ==========*/
              StreamTokenizer tokenizer
                = new StreamTokenizer( new FileReader(FileList[x]) );
              tokenizer.wordChars('_','_' );
              tokenizer.whitespaceChars('=','=');
              while( tokenizer.nextToken()!=StreamTokenizer.TT_EOF )
              {                              //未到文件尾 EOF
                if( !(tokenizer.ttype == StreamTokenizer.TT_WORD) )
                   continue; //直接进行 while 循环的下一次
                //若此 token 不是 word token,就直接找下个 token
                
                tmpStr = tokenizer.sval; //取得此 word token
                if( tmpStr.equals(Text2.getText().trim()) )
                {     
                  List1.add( tmpStr + " " 
                             + FileList[x].getCanonicalPath() );
                  break;  
                }
              }
            } //end of try	
            catch(IOException ecp)
            { 
              JOptionPane.showMessageDialog(Form1,"发生I/O错误!");
            }
          }//end of else if             			    	    
        }//end of for
      }
    } 	
  } //void SelectFiles(File) end
  
  public void SelectFiles2(File tarDir)
  {        //模糊比较   
    if(tarDir.exists())
    {      
      File[] FileList = tarDir.listFiles();
      if( FileList!=null && FileList.length>0)
      {	   
        String tmpLine = null;
        String tmpStr = null;
        for(int x=0; x<FileList.length; x++)
        {	
          if(FileList[x].isDirectory())
             SelectFiles2(FileList[x]);	
          else if( FileList[x].getName().endsWith(".java") )
          {             
            try
            {
              L4.setText( FileList[x].getCanonicalPath() );
              BufferedReader myBuffReader
                = new BufferedReader( new FileReader(FileList[x] ) );
          
              searchFile: 
              while( (tmpLine=myBuffReader.readLine() )!= null  )
              {      					 // 读到一行文字
                /*========== 使用 StringTokenizer ==========*/
                StringTokenizer myTokenizer 
                  = new StringTokenizer(tmpLine);
                while( myTokenizer.hasMoreTokens() ) 
                {     
                  tmpStr = myTokenizer.nextToken(); 
                           //可得一行中的一个片断
                  if( tmpStr.indexOf(Text3.getText().trim())!=-1 )
                  {           //含有 tmpStr 代表的字串
                    List1.add( tmpStr + " " 
                              + FileList[x].getCanonicalPath() );                      
                    break searchFile;
                  }
                }              
              }
              myBuffReader.close();             
            } //end of try
            catch(IOException ecp)
            { 
              JOptionPane.showMessageDialog(Form1,"发生I/O错误!");
            }
          }             			    	    
        } //end of for
      }
    } 	
  } //void SelectFiles2(File) end   
  
  public static void main(String arg[])
  {
    new selectKey();
  }  
}

class JavaFile extends JFrame
{
  JPanel contentPane;
  JTextArea JTextArea1;  
  public JavaFile(String fileName)
  {
    contentPane = (JPanel) this.getContentPane();    
    this.setSize(400, 300);
    this.setTitle(fileName);
    this.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        dispose();
      }    	
    });         
    JTextArea1 = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(JTextArea1);
    contentPane.add(scrollPane, BorderLayout.CENTER);
    try
    {   //将文件内容显示出来
      FileReader myReader = new FileReader(fileName);
      JTextArea1.read(myReader,fileName);
    }
    catch(IOException ecp)
    {
      JOptionPane.showMessageDialog(null,"发生 I/O 错误!"); 
    }
    this.setVisible(true);
  }   
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -