A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© jianhua0798 中级黑马   /  2016-6-11 00:42  /  432 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package day16文件搜索案例;

  2. import java.awt.Dimension;
  3. import java.awt.TextArea;
  4. import java.awt.Toolkit;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.File;

  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.JTextField;

  13. public class FileSearchFrame extends JFrame {
  14.         private JTextField txtDir = new JTextField();//目录文本框
  15.         private JTextField txtKeyword = new JTextField();//关键字文本框
  16.         private TextArea txtResult = new TextArea();//搜索结果文本域
  17.         private JLabel lab3 = new JLabel("文件: 0 个     目录: 0 个");
  18.         private int fileCount = 0;//文件计数器
  19.         private int dirCount = 0;//目录计数器
  20.        
  21.        
  22.         public FileSearchFrame(){
  23.                 init();
  24.                 addComponent();
  25.         }

  26.         private void addComponent() {
  27.                 //1.JLabel--目录:
  28.                 JLabel lab1 = new JLabel("目录:");
  29.                 lab1.setBounds(30, 20, 50, 30);
  30.                 this.add(lab1);
  31.                 //2.JTextField--目录文本框
  32.                 this.txtDir.setBounds(80, 20, 250, 30);
  33.                 this.add(txtDir);
  34.                 //3.JLabel--关键字
  35.                 JLabel lab2 = new JLabel("关键字:");
  36.                 lab2.setBounds(340, 20, 50, 30);
  37.                 this.add(lab2);
  38.                 //4.JTextField--关键字文本框
  39.                 this.txtKeyword.setBounds(390, 20, 100, 30);
  40.                 this.add(this.txtKeyword);
  41.                 //5.JButton--搜索按钮
  42.                 JButton butSearch = new JButton("搜索");
  43.                 butSearch.setBounds(510, 20, 70, 30);
  44.                 //事件处理
  45.                 butSearch.addActionListener(new ActionListener(){
  46.                         @Override
  47.                         public void actionPerformed(ActionEvent e) {
  48.                                 //调用外部的方法
  49.                                 butSearchClick();
  50.                         }});
  51.                 this.add(butSearch);
  52.                
  53.                 //6.java.awt.TextArea-->搜索结果
  54.                 this.txtResult.setBounds(30, 70, 550, 320);
  55.                 this.add(txtResult);
  56.                
  57.                 //7.JLabel--统计标签
  58.                
  59.                 lab3.setBounds(30, 420, 300, 30);
  60.                 this.add(lab3);
  61.                
  62.                
  63.                
  64.         }
  65.         //为搜索按钮添加事件
  66.         protected void butSearchClick() {
  67.                 resetFrame();
  68.                 //1.获取窗体数据
  69.                 String dir = this.txtDir.getText();
  70.                 String keyWord = this.txtKeyword.getText();
  71.                
  72.                 //2.验证是否为空
  73.                 if(dir.trim().length() == 0){
  74.                         JOptionPane.showMessageDialog(this, "请指定搜索内容");
  75.                         return;
  76.                 }
  77.                 if(keyWord.trim().length() ==0){
  78.                         JOptionPane.showMessageDialog(this,"请指定包含的关键字");
  79.                         return;
  80.                 }
  81.                 //3.封装初始化目录
  82.                 File file = new File(dir);
  83.                 //4.判断目录是否存在
  84.                 if(!file.exists() || !file.isDirectory()){
  85.                         JOptionPane.showMessageDialog(this, "请输入有效的文件目录");
  86.                         return;
  87.                 }
  88.                 //5.调用递归方法
  89.                 listFile(file,keyWord);
  90.                 //6.设置统计标签
  91.                 /*System.out.println("fileCount = " + this.fileCount);
  92.                 System.out.println("dirCount = "+ this.dirCount);*/
  93.                 lab3.setText("文件"+fileCount+"个\t目录"+dirCount);
  94.                
  95.                
  96.                
  97.         }
  98.         //每次点击"搜索按钮"时,需要重新设置组件
  99.         private void resetFrame() {
  100.                 //1.清空结果的文本域
  101.                 this.txtResult.setText("");
  102.                 //2.清空两个计数器
  103.                 this.dirCount = 0;
  104.                 this.fileCount = 0;
  105.                 //3.还原统计计数器
  106.                 this.lab3.setText("文件: 0 个   目录: 0 个");
  107.         }

  108.         private void listFile(File file, String key) {
  109.                 //1.获取目录下的所有子文件和子目录的File[]数组
  110.                 File[] fileArray = file.listFiles();
  111.                 if(fileArray != null){
  112.                         //2.遍历
  113.                         for(File f : fileArray){
  114.                                 if(f.getName().contains(key)){
  115.                                         if(f.isDirectory()){
  116.                                                 this.dirCount++;
  117.                                         }
  118.                                        
  119.                                 else{
  120.                                         this.fileCount++;
  121.                                 }
  122.                                 if(this.txtResult.getText().length() != 0){
  123.                                         this.txtResult.append("\r\n");
  124.                                 }
  125.                                 this.txtResult.append(f.getAbsolutePath());
  126.                         }
  127.                                 if(f.isDirectory()){
  128.                                         listFile(f,key);
  129.                                 }
  130.                         }
  131.                        
  132.                 }
  133.         }

  134.         //初始化当前窗体
  135.         private void init() {
  136.                 //1.标题
  137.                 this.setTitle("文件搜索器");
  138.                 //2.设置大小
  139.                 this.setSize(620, 500);
  140.                 //3.设置窗体居中
  141.                 Toolkit kit = Toolkit.getDefaultToolkit();
  142.                 Dimension screenSize = kit.getScreenSize();
  143.                 int x = (screenSize.width - this.getWidth()) / 2;
  144.                 int y = (screenSize.height - this.getHeight()) / 2;
  145.                 this.setLocation(x, y);
  146.                 //4.默认关闭行为
  147.                 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  148.                 //5.设置布局管理器为null
  149.                 this.setLayout(null);
  150.         }
  151. }
复制代码
  1. package day16文件搜索案例;

  2. public class Demo {
  3.         public static void main(String[] args) {
  4.                 new FileSearchFrame().setVisible(true);
  5.         }
  6. }
复制代码

1 个回复

倒序浏览
不错,楼主的分享
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马