java客户端程序—搜寻文件
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.setVisible(true); 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() { @Override public void actionPerformed(ActionEvent e) { path = openFile(); pathText.setText(path); } }); Button confirm = new Button("确认"); confirm.setBackground(Color.white); confirm.addActionListener(new ActionListener() { @Override 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() { @Override 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); } 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 ""; } }