import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;import javax.swing.*;public class SearchTool extends JPanel{
private JLabel lcondition;
private JLabel lpath;
private JLabel lstate;
private JTextField fcondition, fpath;
private JButton search, browse;
private File selectedFile;
private int findCount = 0;
private String c, p;
private JList list;
private DefaultListModel model;
private JScrollPane sp;
public SearchTool(){
lcondition = new JLabel("文件名称: ");
lpath = new JLabel("文件路径:");
lstate = new JLabel("准备查找...");
fcondition = new JTextField(10);
fpath = new JTextField(10);
search = new JButton("搜索");
browse = new JButton("浏览");
model = new DefaultListModel();
list = new JList(model);
list.setLayoutOrientation(JList.VERTICAL);
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int index = list.locationToIndex(e.getPoint());
System.out.println("Double clicked on Item " + index);
Object obj = model.getElementAt(index);
String s = obj.toString();
String s1 = s.substring(0,1);
String s2 = s.substring(1);
/*add " after 2nd char to enable cmd.exe start path which have blank */
s = s1+ "\"" + s2;
try{
String command = "cmd.exe /c " + s;
System.out.println(command);
Runtime.getRuntime().exec(command);
}catch(final IOException err){
err.printStackTrace();
}
}
}
};
list.addMouseListener(mouseListener);
sp = new JScrollPane(list);
// sp.getViewport().setView(list);
browse.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int r = jfc.showDialog(null, "浏览");
if (r == JFileChooser.APPROVE_OPTION){
selectedFile = jfc.getSelectedFile();
fpath.setText(fpath.getText() + ";" +selectedFile.getPath());
}
}
});
fcondition.addKeyListener(new KeyListener(){
public void keyReleased(KeyEvent e){
if(e.getKeyCode()==e.VK_ENTER){
search.doClick();
}
} public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
} public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
});
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints(); gc.anchor = GridBagConstraints.WEST;
addComponet(gc, lcondition, 0, 0, 1, 1);
addComponet(gc, lpath, 0, 1, 1, 1); gc.fill = GridBagConstraints.HORIZONTAL;
gc.weightx = 100;
addComponet(gc, fcondition, 1, 0, 1, 1);
addComponet(gc, fpath, 1, 1, 1, 1); gc.fill = GridBagConstraints.NONE;
gc.weightx = 0;
addComponet(gc, search, 2, 0, 1, 1);
addComponet(gc, browse, 2, 1, 1, 1); gc.fill = GridBagConstraints.HORIZONTAL;
gc.weightx = 100;
addComponet(gc, lstate, 0, 2, 3, 1); gc.fill = GridBagConstraints.BOTH;
gc.weighty = 200;
addComponet(gc, sp, 0, 3, 3, 1);
search.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
// 归位
findCount = 0;
model.clear();
c = fcondition.getText().toLowerCase().trim();
p = fpath.getText().trim();
if ("".equals(c)){
lstate.setText("查找条件不能为空");
}
else if ("".equals(p)){
lstate.setText("查找路径不能为空");
}
else{
Thread th = new Thread(new Runnable(){
public void run(){
String[] path =p.split(";");
for(int i=0; i<path.length; i++){
search40(c,path[i]);
}
lstate.setText("查找结束,一共找到 " + findCount + " 个文件!");
}
});
th.start();
}
}
});
}
public void addComponet(GridBagConstraints gc, Component c, int x, int y,int w, int h){
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = w;
gc.gridheight = h;
add(c, gc);
}
public void search40(String condition, String path){
File f = new File(path);
if (f.exists()){
File[] fs = f.listFiles();
for (int i = 0; i < fs.length; i++){
if (fs[i].getName().equals("System Volume Information"))
continue;
File file = fs[i];
lstate.setText(file.getPath());
if (file.isDirectory()){
search40(condition, file.getPath()); // 递归
} else {
if (file.getName().toLowerCase().indexOf(condition) >= 0){
findCount++;
model.addElement(file.getPath());
}
}
}
} else {
System.out.println("PATH is :" + path);
}
}
public static void main(String s[]){
JFrame frame = new JFrame("File Search");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new SearchTool());
frame.setSize(800,600);
frame.setVisible(true);
}
}
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;import javax.swing.*;public class SearchTool extends JPanel{
private JLabel lcondition;
private JLabel lpath;
private JLabel lstate;
private JTextField fcondition, fpath;
private JButton search, browse;
private File selectedFile;
private int findCount = 0;
private String c, p;
private JList list;
private DefaultListModel model;
private JScrollPane sp;
public SearchTool(){
lcondition = new JLabel("文件名称: ");
lpath = new JLabel("文件路径:");
lstate = new JLabel("准备查找...");
fcondition = new JTextField(10);
fpath = new JTextField(10);
search = new JButton("搜索");
browse = new JButton("浏览");
model = new DefaultListModel();
list = new JList(model);
list.setLayoutOrientation(JList.VERTICAL);
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int index = list.locationToIndex(e.getPoint());
System.out.println("Double clicked on Item " + index);
Object obj = model.getElementAt(index);
String s = obj.toString();
String s1 = s.substring(0,1);
String s2 = s.substring(1);
/*add " after 2nd char to enable cmd.exe start path which have blank */
s = s1+ "\"" + s2;
try{
String command = "cmd.exe /c " + s;
System.out.println(command);
Runtime.getRuntime().exec(command);
}catch(final IOException err){
err.printStackTrace();
}
}
}
};
list.addMouseListener(mouseListener);
sp = new JScrollPane(list);
// sp.getViewport().setView(list);
browse.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int r = jfc.showDialog(null, "浏览");
if (r == JFileChooser.APPROVE_OPTION){
selectedFile = jfc.getSelectedFile();
fpath.setText(fpath.getText() + ";" +selectedFile.getPath());
}
}
});
fcondition.addKeyListener(new KeyListener(){
public void keyReleased(KeyEvent e){
if(e.getKeyCode()==e.VK_ENTER){
search.doClick();
}
} public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
} public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
});
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints(); gc.anchor = GridBagConstraints.WEST;
addComponet(gc, lcondition, 0, 0, 1, 1);
addComponet(gc, lpath, 0, 1, 1, 1); gc.fill = GridBagConstraints.HORIZONTAL;
gc.weightx = 100;
addComponet(gc, fcondition, 1, 0, 1, 1);
addComponet(gc, fpath, 1, 1, 1, 1); gc.fill = GridBagConstraints.NONE;
gc.weightx = 0;
addComponet(gc, search, 2, 0, 1, 1);
addComponet(gc, browse, 2, 1, 1, 1); gc.fill = GridBagConstraints.HORIZONTAL;
gc.weightx = 100;
addComponet(gc, lstate, 0, 2, 3, 1); gc.fill = GridBagConstraints.BOTH;
gc.weighty = 200;
addComponet(gc, sp, 0, 3, 3, 1);
search.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
// 归位
findCount = 0;
model.clear();
c = fcondition.getText().toLowerCase().trim();
p = fpath.getText().trim();
if ("".equals(c)){
lstate.setText("查找条件不能为空");
}
else if ("".equals(p)){
lstate.setText("查找路径不能为空");
}
else{
Thread th = new Thread(new Runnable(){
public void run(){
String[] path =p.split(";");
for(int i=0; i<path.length; i++){
search40(c,path[i]);
}
lstate.setText("查找结束,一共找到 " + findCount + " 个文件!");
}
});
th.start();
}
}
});
}
public void addComponet(GridBagConstraints gc, Component c, int x, int y,int w, int h){
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = w;
gc.gridheight = h;
add(c, gc);
}
public void search40(String condition, String path){
File f = new File(path);
if (f.exists()){
File[] fs = f.listFiles();
for (int i = 0; i < fs.length; i++){
if (fs[i].getName().equals("System Volume Information"))
continue;
File file = fs[i];
lstate.setText(file.getPath());
if (file.isDirectory()){
search40(condition, file.getPath()); // 递归
} else {
if (file.getName().toLowerCase().indexOf(condition) >= 0){
findCount++;
model.addElement(file.getPath());
}
}
}
} else {
System.out.println("PATH is :" + path);
}
}
public static void main(String s[]){
JFrame frame = new JFrame("File Search");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new SearchTool());
frame.setSize(800,600);
frame.setVisible(true);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货