使用java开发文本全局搜索器

ps:推荐大家使用windows builder进行可视化编程。链接:WindowBuilder Pro Eclipse Update Site - http://download.eclipse.org/windowbuilder/WB/release/R201506241200-1/4.4/

下面是文本全局搜索器的代码:

package test;

import java.awt.Button;
import java.awt.EventQueue;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.Font;

public class Main {

	private JFrame frame;
	private JTextField textField;
	private JTextField textField_1;
	private TextArea textArea;
	private String filePath;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main window = new Main();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Main() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("\u6587\u672C\u5168\u5C40\u641C\u7D22\u795E\u5668");
		frame.setResizable(false);
		frame.setBounds(100, 100, 531, 402);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		textField = new JTextField();
		textField.setBounds(10, 10, 368, 21);
		frame.getContentPane().add(textField);
		textField.setColumns(25);

		textField_1 = new JTextField();
		textField_1.setBounds(10, 41, 368, 21);
		frame.getContentPane().add(textField_1);
		textField_1.setColumns(25);

		textArea = new TextArea();
		textArea.setFont(new Font("华文楷体", Font.PLAIN, 14));
		textArea.setBounds(10, 68, 495, 295);
		frame.getContentPane().add(textArea);

		Button button = new Button("\u9009\u62E9\u6587\u4EF6\uFF08\u5939\uFF09");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser jf = new JFileChooser();
				jf.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				jf.showDialog(null, null);
				File file = jf.getSelectedFile();
				try {
					filePath = file.getAbsolutePath();
				} catch (Exception e2) {
				}
				textField.setText(filePath);
			}
		});
		button.setBounds(384, 8, 121, 23);
		frame.getContentPane().add(button);

		Button button_1 = new Button("\u5F00\u59CB\u641C\u7D22");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String keyWord = textField_1.getText().trim().toString();
				if (keyWord == null || keyWord.equals("")) {
					JOptionPane.showMessageDialog(null, "你还未输入搜索内容", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				filePath = textField.getText().trim().toString();
//				textArea.setText("测试"+filePath);
				MySearchThread thread = new MySearchThread(filePath, keyWord);
				thread.start();
			}
		});
		button_1.setBounds(384, 39, 121, 23);
		frame.getContentPane().add(button_1);
	}

	/**
	 * 文件搜索线程类
	 * 
	 * @author shiweixian
	 *
	 */
	class MySearchThread extends Thread {

		String filePath;
		String keyWord;
		List list ;
		public MySearchThread(String filePath, String keyWord) {
			this.filePath = filePath;
			this.keyWord = keyWord;
			list = new ArrayList<>();
		}

		@Override
		public void run() {
			if (filePath == null || filePath.equals("")) {
				JOptionPane.showMessageDialog(null, "无法找到该文件,请重新操作", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			File file = new File(filePath);
			if (file.isFile()) {
				ScannerFile(file); //搜索文件里的内容
			}
			else if(file.isDirectory()){
				try {
					getFilePath(file); //获得文件夹下的所有文件
				} catch (NullPointerException e) {
					JOptionPane.showMessageDialog(null, "路径不正确", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
				}
				if(list != null){
					for(int i = 0;i


你可能感兴趣的:(java,swing,java,windows,可视化编程,搜索)