package cn.itcast.文件搜索器GUI版;
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() {
//目录
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) {
//调用外部方法
butSearchCilck();
}
});
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);
}
//点击按钮将要发生的事
private void butSearchCilck() {
resetFrame();
//获取窗体的数据
String dir = this.txtDir.getText();//文本目录框
String keyword = this.txtKeyword.getText();//关键字文本框
if(dir.trim().length()==0){
JOptionPane.showMessageDialog(this, "请输入关键字!!");
return;
}
if(keyword.trim().length()==0){
JOptionPane.showMessageDialog(this, "请输入关键字索引!");
return;
}
//封装初始目录
File file = new File(dir);
//判断是否是目录并且判断目录是否存在!
if(!file.isDirectory()||!file.exists()){
JOptionPane.showMessageDialog(this, "该目录无效");
return;
}
listFile(file,keyword);
System.out.println("fileCount = " + this.fileCount);
System.out.println("dirCount = " + this.dirCount);
lab3.setText("文件:" + fileCount + " 个 目录:" + 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 keyword) {
File[] fileArray = file.listFiles();
if(fileArray!=null){
for(File h:fileArray){
if(h.getName().contains(keyword)){
if(h.isDirectory()){
this.dirCount++;
}else{
this.fileCount++;
}
if(this.txtResult.getName().length()!=0){
this.txtResult.append("\t\n");
}
this.txtResult.append(h.getAbsolutePath());
}
if(h.isDirectory()){
listFile(h,keyword);
}
}
}
}
protected void init() {
this.setTitle("熊王搜索器");
this.setSize(620,500);
//设置窗体居中
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int x =(screenSize.width-this.getWidth());
int y =(screenSize.height-this.getHeight());
this.setLocation(x, y);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
}
}
|
|