package iotest;
import java.awt.*;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
public class SearchFile
{
public static void main(String[] args)
{
SearchFileFrame frame = new SearchFileFrame();
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class SearchFileFrame extends JFrame
{
public SearchFileFrame()
{
this.setTitle("SearchFileFrame");
setSize(600,450);
Container con = this.getContentPane();
SearchFilePanel p = new SearchFilePanel();
con.add(p);
}
}
class SearchFilePanel extends JPanel
{
private JLabel fileName = new JLabel("查找文件: ");
private JLabel directorName = new JLabel("查找范围: ");
private JLabel process = new JLabel("查找进度... ");
private JButton search = new JButton("开始查找");
private JTextField openfield = new JTextField();
private JTextField savefield = new JTextField();
private JTextArea area = new JTextArea();
private int number =0;
public SearchFilePanel()
{
setLayout(new BorderLayout());
area.setLineWrap(true);
JScrollPane s = new JScrollPane(area);
search.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
area.setText("");
Thread st = new Thread(new Runnable()
{
public void run()
{
searchFile(new File(savefield.getText()),openfield.getText());
}
});
st.start();
}
catch(Exception ee)
{}
}
});
JPanel openpanel = new JPanel();
openpanel.setLayout(new BorderLayout());
openpanel.add(fileName,BorderLayout.WEST);
openpanel.add(openfield,BorderLayout.CENTER);
JPanel savepanel = new JPanel();
savepanel.setLayout(new BorderLayout());
savepanel.add(directorName,BorderLayout.WEST);
savepanel.add(savefield,BorderLayout.CENTER);
JPanel control = new JPanel();
control.setLayout(new BorderLayout());
control.add(openpanel,BorderLayout.NORTH);
control.add(savepanel,BorderLayout.SOUTH);
JPanel searchPanel = new JPanel();
searchPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
searchPanel.add(search);
searchPanel.add(process);
JPanel NorPanel = new JPanel();
NorPanel.setLayout(new BorderLayout());
NorPanel.add(control,BorderLayout.NORTH);
NorPanel.add(searchPanel,BorderLayout.SOUTH);
add(NorPanel,BorderLayout.NORTH);
add(s,BorderLayout.CENTER);
}
public void searchFile(File file,String fileName) //核心递归算法
{
if(file.exists())
{
File[] f = file.listFiles();
if(f!=null)
{
for(int i =0 ;i<f.length;i++)
{
process.setText(f[i].getPath());
if(f[i].isDirectory())
{
searchFile(f[i],fileName);
}
else
{
String temp = f[i].getName();
if(temp.indexOf(fileName)!=-1)
{
area.append(f[i].getPath()+"/r/n");
}
}
}
}
}
}
}