1 packagemain;2
3 importjava.awt.Dimension;4 importjava.awt.Toolkit;5 importjava.awt.event.ActionEvent;6 importjava.awt.event.ActionListener;7 importjava.io.File;8 importjava.io.FilenameFilter;9 importjava.text.SimpleDateFormat;10 importjava.util.Arrays;11
12 importjavax.swing.JButton;13 importjavax.swing.JComboBox;14 importjavax.swing.JFileChooser;15 importjavax.swing.JFrame;16 importjavax.swing.JLabel;17 importjavax.swing.JPanel;18 importjavax.swing.JScrollPane;19 importjavax.swing.JTable;20 importjavax.swing.JTextField;21 importjavax.swing.UIManager;22 importjavax.swing.UnsupportedLookAndFeelException;23 importjavax.swing.event.DocumentEvent;24 importjavax.swing.event.DocumentListener;25 importjavax.swing.filechooser.FileSystemView;26 importjavax.swing.table.DefaultTableModel;27
28 public class FrmFind extendsJFrame {29 private JLabel lblPath; //提示输入路径
30 private JTextField txtPath; //选择后显示路径
31 private JFileChooser fchPath; //文件夹选择
32 private JButton btnPath; //文件夹选择按钮
33 private JLabel lblFix; //后缀选择
34 private JTextField txtFix; //后缀显示
35 private JComboBox comFix; //已有后缀选择
36 private JButton btnFix; //添加到过滤后缀列表
37 private JTable tabFile; //符合条件的文件列表
38 private JPanel pnlFile; //文件列表承载面板(列表为tabFile,它会被放置到下面的JScrollPane)
39 private JScrollPane jspFile; //文件列表承载面板,用来承载tabFile,而被pnlFile承载
40
41 /**
42 * 布局和属性设置43 */
44 private voidinitLayoutAndProperties() {45 setTitle("文件查找");46 setSize(800, 600);47 setDefaultCloseOperation(EXIT_ON_CLOSE);48 setLayout(null);49
50 //设置窗体居中
51 Toolkit kit =Toolkit.getDefaultToolkit();52 Dimension screenSize =kit.getScreenSize();53 int screenWidth =screenSize.width;54 int screenHeight =screenSize.height;55 int windowWidth = this.getWidth();56 int windowHeight = this.getHeight();57 setLocation(screenWidth / 2 - windowWidth / 2, screenHeight / 2
58 - windowHeight / 2);59 }60
61 /**
62 * 组件设置63 */
64 private voidinitComponents() {65 //lblPath
66 lblPath = newJLabel();67 lblPath.setSize(72, 12); //卖了一下萌,一个个像素测试的,下面的也是……
68 lblPath.setLocation(30, 18);69 lblPath.setText("请选择路径:");70 //txtPath
71 txtPath = newJTextField();72 txtPath.setSize(400, 24);73 txtPath.setLocation(102, 13);74 txtPath.setEditable(false);75 txtPath.setText(FileSystemView.getFileSystemView().getHomeDirectory()76 .toString()); //默认显示桌面路径77 //fchPath
78 fchPath = newJFileChooser();79 fchPath.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); //文件浏览窗体只能选择文件夹打开
80 fchPath.setApproveButtonText("确定");81 fchPath.setCurrentDirectory(FileSystemView.getFileSystemView()82 .getHomeDirectory()); //文件浏览窗体默认打开桌面83 //btnPath
84 btnPath = newJButton();85 btnPath.setText("选 择");86 btnPath.setSize(55, 24);87 btnPath.setLocation(510, 13);88 btnPath.addActionListener(newActionListener() {89
90 @Override91 public voidactionPerformed(ActionEvent e) {92 btnPath_Click();93 }94 });95 //lblFix
96 lblFix = newJLabel();97 lblFix.setText("类型选择:");98 lblFix.setLocation(30, 48);99 lblFix.setSize(60, 12);100 //txtFix
101 txtFix = newJTextField();102 txtFix.setSize(400, 24);103 txtFix.setLocation(90, 42);104 txtFix.setToolTipText("您可以手动录入,请以“;”分开多个后缀");105 txtFix.getDocument().addDocumentListener(new DocumentListener() { //后缀的文本框文本发生改变时引发事件,都更新列表
106
107 @Override108 public voidremoveUpdate(DocumentEvent e) {109 loadList();110 }111
112 @Override113 public voidinsertUpdate(DocumentEvent e) {114 loadList();115 }116
117 @Override118 public voidchangedUpdate(DocumentEvent e) {119 loadList();120 }121 });122 //comFix
123 String[] preList = { "jpg", "mp3", "rmvb", "txt", "torrent" }; //默认的后缀列表,可以增加或删减
124 comFix = newJComboBox(preList);125 comFix.setEditable(false);126 comFix.setSelectedIndex(0);127 comFix.setSize(60, 24);128 comFix.setLocation(500, 42);129 //btnFix
130 btnFix = newJButton();131 btnFix.setText("添加到过滤列表");132 btnFix.setSize(112, 24);133 btnFix.setLocation(570, 42);134 btnFix.addActionListener(newActionListener() {135
136 @Override137 public voidactionPerformed(ActionEvent e) {138 btnFix_Click();139 }140 });141 //tabFile
142 tabFile = new JTable(new DefaultTableModel(null, new String[] { "文件名",143 "类型", "大小", "最后修改时间"}) {144
145 public boolean isCellEditable(int row, intcolumn) {146 return false;147 }148 });149 //pnlFile
150 pnlFile = newJPanel();151 pnlFile.setLocation(30, 80);152 pnlFile.setSize(720, 460);153 //jspFile
154 jspFile = newJScrollPane();155 jspFile.setViewportView(tabFile);156 jspFile.setPreferredSize(pnlFile.getSize()); //到凌晨3点半才搞定,这个主要是我对Java157 //Swing的布局非常不熟悉
158
159 add(lblPath);160 add(txtPath);161 add(btnPath);162 add(lblFix);163 add(txtFix);164 add(comFix);165 add(btnFix);166 pnlFile.add(jspFile);167 add(pnlFile);168 }169
170 /**
171 * “选择”按钮点击事件172 */
173 private voidbtnPath_Click() {174 int result = 0;175 result = fchPath.showOpenDialog(this);176 if (JFileChooser.APPROVE_OPTION ==result) {177 txtPath.setText(fchPath.getSelectedFile().getPath());178 loadList();179 }180 }181
182 /**
183 * “添加到过滤列表”按钮点击事件184 */
185 private voidbtnFix_Click() {186 if (txtFix.getText().indexOf(comFix.getSelectedItem().toString()) == -1) {187 String fixFull =txtFix.getText();188 if (!txtFix.getText().equals(""))189 fixFull += ";";190 fixFull +=comFix.getSelectedItem().toString();191 txtFix.setText(fixFull);192 }193 }194
195 /**
196 * 通过选择路径和过滤后缀来加载符合条件的文件列表197 */
198 private voidloadList() {199 File fPath = new File(txtPath.getText()); //获取文件夹路径
200 File[] files = null; //按需获取文件
201 if (!txtFix.getText().equals("")) { //需要过滤后缀的情况
202 final String[] fixs = txtFix.getText().split(";"); //按照不同格式,由于上文规定,此处即使用";"拆分多个后缀
203 Arrays.sort(fixs); //如果需要使用如下数组内查询对象的方法,则需要现将数组进行排序,这点很重要
204 files = fPath.listFiles(newFilenameFilter() {205
206 @Override207 public booleanaccept(File dir, String name) {208 returnArrays.binarySearch(fixs,209 name.substring(name.lastIndexOf('.') + 1)) >= 0;210 }211 });212 } else
213 //不需要过滤后缀的情况
214 files =fPath.listFiles();215 DefaultTableModel dtm = (DefaultTableModel) tabFile.getModel(); //开始对表格数据进行操作,先得到表模型
216 dtm.setRowCount(0); //清除原数据
217 for (File file : files) { //将文件信息逐个添加到行,此处我没有详细推敲
218 dtm.insertRow(219 dtm.getRowCount(),220 newString[] {221 file.getName(),222 file.isDirectory() ? "文件夹"
223 : file.getName()224 .substring(225 file.getName().lastIndexOf(226 '.') + 1)227 + "文件",228 file.length() % 1024 == 0 ? file.length() / 1024
229 + "KB" : file.length() / 1024 + 1 + "KB",230 new SimpleDateFormat("yyyy年MM月dd日 HH:mm")231 .format(file.lastModified()) });232 }233 }234
235 publicFrmFind() {236 initLayoutAndProperties();237 initComponents();238 loadList();239 }240
241 public static void main(String[] args) throwsClassNotFoundException,242 InstantiationException, IllegalAccessException,243 UnsupportedLookAndFeelException {244 UIManager245 .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); //设置程序UI模式,其实Java也就只自定义了几种,看源代码就能够完全了解
246 new FrmFind().setVisible(true);247 }248 }