java客户端程序—搜索文件

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

/**
 *       @author itace
 * 
 *         2011-10-27
 */

public class FindFile {

	private static TextField pathText = null;
	private static TextField nameText = null;
	private static String path = "";
	private static String filename = "";
	private static TextArea area = null;

	public static void main(String[] args) {

		JFrame jf = new JFrame("hello");
		jf.setSize(300, 160);
		jf.setLocation(390, 200);
		
		jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
		
		
		Container con = jf.getContentPane();
		con.setLayout(new BorderLayout());

		con.add(new Label("   "), BorderLayout.SOUTH);
		con.add(new Label("   "), BorderLayout.WEST);
		con.add(new Label("   "), BorderLayout.EAST);
		con.add(new Label("   "), BorderLayout.NORTH);

		Panel p = new Panel();
		p.setLayout(new GridLayout(3, 3));

		// 组件
		pathText = new TextField("", 10);
		pathText.setEnabled(false);

		nameText = new TextField("", 10);// 文件名

		Button b = new Button("浏览…");
		b.setSize(20, 20);
		b.setBackground(Color.white);
		b.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

				path = openFile();
				pathText.setText(path);
			}
		});
		Button confirm = new Button("确认");
		confirm.setBackground(Color.white);
		confirm.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				filename = nameText.getText();
				File f = new File(path);
				if (f.exists()) {
					JFrame frame = new JFrame("搜索到的文件");
					frame.setSize(600, 600);
					frame.setLocation(390, 200);
					frame.setVisible(true);
					Container cc = frame.getContentPane();

					area = new TextArea();
					cc.add(area);

					showFile(f, filename);
					area.append("--------------------------------------------\n");
					area.append("-------------搜索------完毕-------------------\n");
					area.append("--------------------------------------------");
				} else {
					
				}
			}
		});
		Button cancel = new Button("取消");
		cancel.setBackground(Color.white);
		cancel.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				System.exit(0);
				
			}
		});
		ComponentOrientation o = ComponentOrientation.LEFT_TO_RIGHT;
		p.setComponentOrientation(o);

		p.add(new Label("文件名"));
		p.add(nameText);
		p.add(new Label("   "));
		p.add(new Label("文件夹"));
		p.add(pathText);
		p.add(b);
		p.add(new Label("   "));
		p.add(confirm);
		p.add(cancel);

		con.add(p, BorderLayout.CENTER);
		jf.setVisible(true);
	}

	public static void showFile(File f, String filename) {
		boolean bool = f.isDirectory();
		if (bool) {
			File[] file = f.listFiles();
			if (file != null && file.length > 0) {
				
				for (int i = 0; i < file.length; ++i) {
					File fe = file[i];
					String name = fe.getName();
					int t = name.indexOf(filename);
					if (t != -1) {
						String zhen = fe.getPath() + "\n";
						// System.out.println(zhen);
						area.append(zhen);
					}
					boolean boo = file[i].isDirectory();
					if (boo) {
						showFile(fe, filename);
					}
				}
			}
		}
	}

	// 弹出选择框返回选中文件夹的路径
	public static String openFile() {
		JFrame frame = new JFrame();
		String filename = File.separator + "temp";
		File f = new File(filename);
		JFileChooser fc = new JFileChooser(f);

		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int result = fc.showOpenDialog(frame);
		if (result == 0) {
			File selFile = fc.getSelectedFile();
			return selFile.toString();
		}
		return "";
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值