資源簡介
java實現根據文件名查找本地文件,該程序可直接放在eclipse或者Myeclipse中則可運行,不需要下載其他什么工具包,為開發人員節省了很多時間
代碼片段和文件信息
package?test;
import?java.awt.Component;
import?java.awt.GridBagConstraints;
import?java.awt.GridBagLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.io.File;
import?javax.swing.JButton;
import?javax.swing.JFileChooser;
import?javax.swing.Jframe;
import?javax.swing.JLabel;
import?javax.swing.JPanel;
import?javax.swing.JScrollPane;
import?javax.swing.JTextArea;
import?javax.swing.JTextField;
public?class?UIfileSearch?extends?Jframe?{
????
???public?UIfileSearch()?{
????????this.settitle(“文件搜索JAVA實現“);
????????this.setSize(600400);
????????this.setLocationRelativeTo(null);
????????this.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
????????this.add(new?SearchFilePanel());
????}
????
????public?static?void?main(String[]?args)?{
????????UIfileSearch?test?=?new?UIfileSearch();
????System.out.println(“5“);
????????test.setVisible(true);
????}
}
class?SearchFilePanel?extends?JPanel{
????
????/**
?????*?路徑瀏覽和查找按鈕?
?????*?Buttons?for?browsing?the?file?system?and?for?starting?the?search
????*/
????private?JButton?browsesearch;
???/**
????*?查找條件,路徑,查找過程
????*?the?conditionsdirectoryprocess?of?Searching
?????*/
????private?JLabel?filter?directorystatusShow1statusShow2;
????/**
?????*?輸入查找條件和路徑的文本框
?????*?TextField?for?inputing?the?search?filter?and?the?file?path
?????*/
????private?JTextField?textFiltertextDirectory;
????/**
?????*?顯示查找結果的文本區
?????*?the?textArea?to?show?the?result?of?searching
?????*/
????private?JTextArea?result;
????
????
????private?File?selectedFile;
???/**
????*?找到的文件數量
????*?number?of?file?had?found
????*/
????private?long?countFiles;
????/**
????*?temporary?variable?to?store?the??and?
????*/
????private?String?p?f;
???
????public??SearchFilePanel()?{
???????
???????filter?=?new?JLabel(“全部或者部分文件名:“2);
???????directory?=?new?JLabel(“查找:“2);
???????statusShow1?=?new?JLabel(“準備收索...“2);
???????statusShow2?=?new?JLabel(“被發現的文件數:“);
????????textFilter?=?new?JTextField(15);
????????textDirectory?=?new?JTextField(15);
???????result?=?new?JTextArea();
????????result.setEditable(false);
????????JScrollPane?js?=?new?JScrollPane(result);
????????
????????browse?=?new?JButton(“瀏覽“);
???????search?=?new?JButton(“開始收索“);
???????/**
????????*?給瀏覽按鈕設置監聽事件
?????????*?add?ActionListener?for?button?browse ?directly
?????????*/
???????browse.addActionListener(new?ActionListener(){
???????????public?void?actionPerformed(ActionEvent?evt)?{
???????????????JFileChooser?jfc?=?new?JFileChooser();
??????????????//Just?choose?a?directory?address
??????????????jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
???????????????int?d?=?jfc.showDialog(null?“Browse?the?files“);
???????????????if(d?==?JFileChooser.APPROVE_OPTION)?{
????????????????????selectedFile?=?jfc.getSelectedFile();
????????????????????textDirectory.setText(selectedFile.getPath());
????????????????}
??????
評論
共有 條評論