TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
- [img]http://img.javaxxz.com/2014/11/4/235603031.jpg[/img]
- import java.io.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class FindFrame extends JFrame
- {
- public FindFrame()
- {
- this.setTitle("本地文件搜索引擎");
- this.setSize(600, 400);
- this.setLocationRelativeTo(null);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
- this.add(new FindPanel());
- }
- public static void main(String[] args)
- {
- FindFrame frame=new FindFrame();
- frame.setVisible(true);
- }
- }
- class FindPanel extends JPanel
- {
- private JLabel findLabel, pathLabel;
- private JTextField findField, showPathField;
- private JComboBox pathBox;
- private JTextArea resultArea;
- private JButton findButton;
-
- public FindPanel()
- {
- this.setLayout(null);
- findLabel=new JLabel("全部或部分文件名");
- findLabel.setFont(new Font("仿宋", Font.BOLD, 20));
- findLabel.setSize(200, 60);
- findLabel.setLocation(10, 10);
-
- findField=new JTextField();
- findField.setSize(200, 25);
- findField.setLocation(10, 55);
-
- pathLabel=new JLabel("在这里寻找");
- pathLabel.setFont(new Font("仿宋", Font.BOLD, 20));
- pathLabel.setSize(200, 60);
- pathLabel.setLocation(10, 65);
-
- String[] pathString={"C:", "D:", "E:", "F:", "H:", "I:", "J:", "K:", "L:"};
- pathBox=new JComboBox(pathString);
- pathBox.setSize(200, 25);
- pathBox.setLocation(10, 110);
-
- findButton=new JButton("搜索");
- findButton.setFont(new Font("仿宋", Font.BOLD, 15));
- findButton.setSize(80, 30);
- findButton.setLocation(65, 145);
-
- resultArea=new JTextArea();
- resultArea.setEnabled(false);
- resultArea.setFont(new Font("宋体", Font.PLAIN, 20));
-
- JScrollPane area=new JScrollPane(resultArea);
- area.setSize(350, 330);
- area.setLocation(220, 10);
-
- showPathField=new JTextField();
- showPathField.setEditable(false);
- showPathField.setSize(600, 25);
- showPathField.setLocation(0, 340);
-
- this.add(findLabel);
- this.add(findField);
- this.add(pathLabel);
- this.add(pathBox);
- this.add(findButton);
- this.add(area);
- this.add(showPathField);
-
- findButton.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0)
- {
- resultArea.setText(null); //清空文本区
- //线程
- new Thread()
- {
- public void run()
- {
- listfile((String)pathBox.getSelectedItem()+File.separator);
- }
- }.start();
- }
- });
- }
- //构建一个取得抽象路径名数组的方法
- public void listfile(String path)
- {
- //初始化并创建文件类
- File file=new File(path);
-
- //调用所创建的过滤器方法
- File[] files=file.listFiles(new MyFile());
-
- for(int i=0; i< files.length; i++)
- {
- //如果此抽象路径名表示的文件是一个标准文件。
- if(files[i].isFile())
- {
- //将抽象路径名转换为一个路径名字符串并在显示路径名文本域中显示出来
- showPathField.setText(files[i].getPath());
- //取得该路径下的文件(父抽象路径名,子路径名字符串)并在文本区中依次显示
- resultArea.append("
- "+files[i].getAbsolutePath());
- }
- /*
- *如果此抽象路径名所表示的文件是一个目录并且此抽象路径名所
- 表示文件或目录名称与所输入的文件名不相同
- */
- else if(files[i].isDirectory() && !(files[i].getName().equals(findField.getText())))
- {
- //将抽象路径名转换为一个路径名字符串并在显示路径名文本域中显示出来
- showPathField.setText(files[i].getPath());
- //取得该抽象路径名数组的绝对路径名字符串
- listfile(files[i].getAbsolutePath());
- }
-
- /*
- *如果此抽象路径名所表示的文件是一个目录并且此抽象路径名所
- 表示文件或目录名称与所输入的文件名相同
- */
- else if(files[i].isDirectory() && (files[i].getName().equals(findField.getText())))
- {
- //将抽象路径名转换为一个路径名字符串并在显示路径名文本域中显示出来
- showPathField.setText(files[i].getPath());
- //取得该抽象路径名数组的绝对路径名字符串并在文本区中依次显示
- resultArea.append("
- "+files[i].getAbsolutePath());
-
- //取得该抽象路径名数组的绝对路径名字符串
- listfile(files[i].getAbsolutePath());
- }
-
- /*
- * 如果此抽象路径名所表示的文件不是一个目录或文件并且此抽象路径名所
- 表示的文件或目录名称与所输入的文件名相同
- */
- else if(!(files[i].isDirectory()) && files[i].getName().equals(findField.getText()))
- {
- //将抽象路径名转换为一个路径名字符串并在显示路径,名文本域中显示出来
- showPathField.setText(files[i].getPath());
-
- //取得该抽象路径名数组的绝对路径名字符串并在文本区中依次显示
- resultArea.append("
- "+files[i].getAbsolutePath());
- }
-
- else
- {
- //将抽象路径名转换为一个路径名字符串并在显示路径,名文本域中显示出来
- showPathField.setText(files[i].getPath());
- //取得该抽象路径名数组的绝对路径名字符串
- listfile(files[i].getAbsolutePath());
- }
- }
- }
-
- /*
- * 使用FilenameFilter接口创建一个用于过滤器文件名的类实例。并使用这些实例过滤
- File类的list方法中的目录清单。(添加创建一个过滤器)
- */
- class MyFile implements FilenameFilter
- {
- /*
- * 测试指定文件是否应该包含在某一文件列表中(dir:被找到的文件所在的目录。
- name:文件的名称。)
- */
- public boolean accept(File dir, String name)
- {
- //初始化该类组件
- File file=new File(dir, name);
-
- /*
- * 判断此抽象路径名表示的文件是否是一个目录,或者取得由此抽象路径名
- 表示的文件或目录的名称并判断用户所输入框中的文本是否为后缀名。
- */
- return (file.isDirectory() || file.getName().endsWith(findField.getText()));
- }
- }
- }
复制代码
源码下载:http://file.javaxxz.com/2014/11/4/235603265.zip |
|