- package day16文件搜索案例;
- import java.awt.Dimension;
- import java.awt.TextArea;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- public class FileSearchFrame extends JFrame {
- private JTextField txtDir = new JTextField();//目录文本框
- private JTextField txtKeyword = new JTextField();//关键字文本框
- private TextArea txtResult = new TextArea();//搜索结果文本域
- private JLabel lab3 = new JLabel("文件: 0 个 目录: 0 个");
- private int fileCount = 0;//文件计数器
- private int dirCount = 0;//目录计数器
-
-
- public FileSearchFrame(){
- init();
- addComponent();
- }
- private void addComponent() {
- //1.JLabel--目录:
- JLabel lab1 = new JLabel("目录:");
- lab1.setBounds(30, 20, 50, 30);
- this.add(lab1);
- //2.JTextField--目录文本框
- this.txtDir.setBounds(80, 20, 250, 30);
- this.add(txtDir);
- //3.JLabel--关键字
- JLabel lab2 = new JLabel("关键字:");
- lab2.setBounds(340, 20, 50, 30);
- this.add(lab2);
- //4.JTextField--关键字文本框
- this.txtKeyword.setBounds(390, 20, 100, 30);
- this.add(this.txtKeyword);
- //5.JButton--搜索按钮
- JButton butSearch = new JButton("搜索");
- butSearch.setBounds(510, 20, 70, 30);
- //事件处理
- butSearch.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- //调用外部的方法
- butSearchClick();
- }});
- this.add(butSearch);
-
- //6.java.awt.TextArea-->搜索结果
- this.txtResult.setBounds(30, 70, 550, 320);
- this.add(txtResult);
-
- //7.JLabel--统计标签
-
- lab3.setBounds(30, 420, 300, 30);
- this.add(lab3);
-
-
-
- }
- //为搜索按钮添加事件
- protected void butSearchClick() {
- resetFrame();
- //1.获取窗体数据
- String dir = this.txtDir.getText();
- String keyWord = this.txtKeyword.getText();
-
- //2.验证是否为空
- if(dir.trim().length() == 0){
- JOptionPane.showMessageDialog(this, "请指定搜索内容");
- return;
- }
- if(keyWord.trim().length() ==0){
- JOptionPane.showMessageDialog(this,"请指定包含的关键字");
- return;
- }
- //3.封装初始化目录
- File file = new File(dir);
- //4.判断目录是否存在
- if(!file.exists() || !file.isDirectory()){
- JOptionPane.showMessageDialog(this, "请输入有效的文件目录");
- return;
- }
- //5.调用递归方法
- listFile(file,keyWord);
- //6.设置统计标签
- /*System.out.println("fileCount = " + this.fileCount);
- System.out.println("dirCount = "+ this.dirCount);*/
- lab3.setText("文件"+fileCount+"个\t目录"+dirCount);
-
-
-
- }
- //每次点击"搜索按钮"时,需要重新设置组件
- private void resetFrame() {
- //1.清空结果的文本域
- this.txtResult.setText("");
- //2.清空两个计数器
- this.dirCount = 0;
- this.fileCount = 0;
- //3.还原统计计数器
- this.lab3.setText("文件: 0 个 目录: 0 个");
- }
- private void listFile(File file, String key) {
- //1.获取目录下的所有子文件和子目录的File[]数组
- File[] fileArray = file.listFiles();
- if(fileArray != null){
- //2.遍历
- for(File f : fileArray){
- if(f.getName().contains(key)){
- if(f.isDirectory()){
- this.dirCount++;
- }
-
- else{
- this.fileCount++;
- }
- if(this.txtResult.getText().length() != 0){
- this.txtResult.append("\r\n");
- }
- this.txtResult.append(f.getAbsolutePath());
- }
- if(f.isDirectory()){
- listFile(f,key);
- }
- }
-
- }
- }
- //初始化当前窗体
- private void init() {
- //1.标题
- this.setTitle("文件搜索器");
- //2.设置大小
- this.setSize(620, 500);
- //3.设置窗体居中
- Toolkit kit = Toolkit.getDefaultToolkit();
- Dimension screenSize = kit.getScreenSize();
- int x = (screenSize.width - this.getWidth()) / 2;
- int y = (screenSize.height - this.getHeight()) / 2;
- this.setLocation(x, y);
- //4.默认关闭行为
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //5.设置布局管理器为null
- this.setLayout(null);
- }
- }
复制代码- package day16文件搜索案例;
- public class Demo {
- public static void main(String[] args) {
- new FileSearchFrame().setVisible(true);
- }
- }
复制代码
|
|