简单的文档查看器(程序员使用工具)

类别:Java 点击:0 评论:0 推荐:

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TestSearch extends JFrame implements ActionListener{
 private JTextField tf1,tf2;
 private JButton b1,b2;
 private JTextArea ta;
 private JPanel p,p1,p2;
 private JLabel l1,l2;
 public TestSearch(){
  super("Search");
  Container c=getContentPane();
  p=new JPanel(new GridLayout(2,1));
  p1=new JPanel();
  p2=new JPanel();
  l1=new JLabel("要查找的文件");
  l2=new JLabel("要查找的内容");
  ta=new JTextArea();
  tf1=new JTextField(20);
  tf2=new JTextField(18);
  b1=new JButton("浏览");
  b2=new JButton("开始查找");
  c.setLayout(new BorderLayout());
  c.add(BorderLayout.NORTH,p);
  c.add(BorderLayout.CENTER,new JScrollPane(ta));
  p1.add(l1);
  p1.add(tf1);
  p1.add(b1);
  p2.add(l2);
  p2.add(tf2);
  p2.add(b2);
  p.add(p1);
  p.add(p2);
  b1.addActionListener(this);
  b2.addActionListener(this);
  setSize(400,400);
  show();
 
 
 
 }
 public static void main(String args[]){
         new TestSearch(); 
 }

 public void actionPerformed(ActionEvent parm1) {
  if (parm1.getSource()==b1)
      {
       JFileChooser chooser=new JFileChooser();
       chooser.setDialogType(JFileChooser.OPEN_DIALOG);
       chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
       int i=chooser.showDialog(null,null);
       if(i==JFileChooser.APPROVE_OPTION){
        tf1.setText(chooser.getSelectedFile().getAbsoluteFile().toString());
       }
       
      }
  if(parm1.getSource()==b2)
   {  ta.setText("");
    String str1=tf1.getText();
    String str2=tf2.getText();
    int i=1;
    try{
    BufferedReader breader=new BufferedReader(new FileReader(str1));
    while(true){
     String str3=breader.readLine();
     if(str3.indexOf(str2)!=-1){
      ta.append("\""+str2+"\""+"在文件的第"+i+"行!\n");
      i++;      
      continue;
     }
     i++;
      if(str3==null)
      { ta.append("收索完毕");
       break;
       }
          
     }
    }catch(Exception e){}

    
   }
 }
}

本文地址:http://com.8s8s.com/it/it16755.htm