【发布时间】:2014-03-23 18:52:08
【问题描述】:
我正在尝试在用户选择的目录中搜索所有文件。有一个选项可以搜索特定的字符串,并且只有带有这些字符实例的文件名出现在屏幕上。然而,当我运行这个程序时,屏幕上没有出现任何文本(除了使用 JTextArea 构造函数指定的默认值)。如果可能的话,如果有人帮助我调试,我将不胜感激。
package comp.search.direct;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JCheckBox;
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;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
@SuppressWarnings("serial")
public class SearchInitializer extends JFrame implements ActionListener {
public static JButton button;
public static JTextArea ta;
public static JTextField tf;
public static JPanel pane, pane2, pane3;
public static JLabel label;
public static JCheckBox jcb;
@SuppressWarnings({ "unchecked", "rawtypes" })
public static ArrayList<String> filenames = new ArrayList();
@SuppressWarnings("unused")
public static void main(String[] args) {
SearchInitializer sci = new SearchInitializer();
}
public SearchInitializer() {
super("Search Directory");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
this.setLayout(new BorderLayout());
pane = new JPanel();
pane.setLayout(new BorderLayout());
button = new JButton("Search Directory");
ta = new JTextArea("File Names:");
ta.setEditable(false);
tf = new JTextField(10);
label = new JLabel("Find:");
jcb = new JCheckBox("Append Text");
pane2 = new JPanel();
pane2.add(label);
pane2.add(tf);
pane3 = new JPanel();
pane3.add(button);
pane3.add(jcb);
pane.add(pane3, BorderLayout.CENTER);
pane.add(pane2, BorderLayout.EAST);
addActionListeners();
getContentPane().add(pane, BorderLayout.NORTH);
getContentPane().add(new JScrollPane(ta), BorderLayout.CENTER);
this.setResizable(true);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
}
public static void searchDirectory(File folder, JTextField find, JTextArea ta, ArrayList<String> arg) {
arg.clear();
for (File fileentry : folder.listFiles()) {
if (find.getText() == null) {
if (fileentry.isDirectory()) {
searchDirectory(fileentry, find, ta, arg);
} else {
if (!fileentry.getName().endsWith(".lnk"))
ta.append("\n" + fileentry.getName());
}
} else {
if (fileentry.isDirectory()) {
searchDirectory(fileentry, find, ta, arg);
} else {
if (!fileentry.getName().contains(find.getText()))
return;
else {
if (arg.contains(fileentry.getName())) {
ta.append("\n" + fileentry.getName());
arg.add(fileentry.getName());
searchDirectory(folder, find, ta, arg);
System.out.println(fileentry.getName()); // Testing purposes only
} else {
return;
}
}
}
}
}
}
public void addActionListeners() {
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fc.showDialog(SearchInitializer.this, "Select") == JFileChooser.APPROVE_OPTION) {
searchDirectory(new File(fc.getSelectedFile().getAbsolutePath()), tf, ta, filenames);
}
}
}
}
【问题讨论】:
-
不要在你的问题中加入不相关的代码。
-
如果我认为这段代码无关紧要,我就不会包含它。我试图通过包含尽可能多的代码来提供帮助。我很抱歉。
标签: java file search directory