1、界面
一看到这个界面就这个窗体是一个空布局,界面其实很简单一个文本输入框、按钮和文本输入域
2、功能
按钮监听,一点击这个按钮就是列出所有该路径文件夹下的子文件
3、具体代码:
package FileSreach;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
* 界面
* @author Administrator
*
*/
public class FileSreachFrame extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JTextArea jta;
private JTextField jtf;
public static void main(String[] args){
new FileSreachFrame().initSreachFrame();
}
private void initSreachFrame(){
this.setSize(500,500);
this.setTitle("文件搜索器");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLayout(null);
jtf = new JTextField();
jtf.setBounds(50,25,250,35);
JButton button = new JButton("开始搜索");
button.setBounds(350,25,100,35);
button.addActionListener(this);
jta = new JTextArea();
JScrollPane jsp = new JScrollPane(jta);
jsp.setBounds(50,125,400,300);
this.add(jtf);
this.add(button);
this.add(jsp);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("开始搜索")){
FileSreachFunction sreach = new FileSreachFunction(jtf,jta);
// jta.removeAll();
// FileSreachFrame.this.repaint();
}
}
}
package FileSreach;
import java.io.File;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
* 实现
* @author Administrator
*
*/
public class FileSreachFunction {
private JTextArea jta;
public FileSreachFunction(JTextField jtf,JTextArea jta){
this.jta = jta;
this.initSreachFunction(jtf.getText());
}
private void initSreachFunction(String path){
File file = new File(path);
if(file.exists()&&file.length()!=0){
if(file.isFile()){
jta.append(file.getAbsolutePath()+"\r\n");
}else{
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
String absolute = files[i].getAbsolutePath();
jta.append(absolute+"\r\n");
this.initSreachFunction(absolute);
}
}
}
}
}