我想要一个 JTree,它显示我当前目录中只有“.h”扩展名的所有文件,它应该显示所有文件夹,除了一个名为“系统卷信息”的文件夹,在我的当前目录中,这是我的代码,什么补充..我需要做的更新?
import java.awt.BorderLayout;
import java.awt.Container;
import java.io.File;
import java.util.Collections;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class DriveTree extends JPanel
{
public DriveTree(File dir)
{
setLayout(new BorderLayout());
JTree tree = new JTree(addNodes(null, dir));
add(tree);
}
DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir)
{
String curPath = dir.getPath();
DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
if (curTop != null)
{
curTop.add(curDir);
}
Vector ol = new Vector();
String[] tmp = dir.list();
for (int i = 0; i < tmp.length; i++)
ol.addElement(tmp[i]);
Collections.sort(ol, String.CASE_INSENSITIVE_ORDER);
File f;
Vector files = new Vector();
for (int i = 0; i < ol.size(); i++)
{
String thisObject = (String) ol.elementAt(i);
String newPath;
if (curPath.equals("."))
newPath = thisObject;
else
newPath = curPath + File.separator + thisObject;
if ((f = new File(newPath)).isDirectory())
addNodes(curDir, f);
else
files.addElement(thisObject);
}
for (int fnum = 0; fnum < files.size(); fnum++)
curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
return curDir;
}
public static void main(String[] av)
{
JFrame frame = new JFrame("Drive View");
Container cp = frame.getContentPane();
if (av.length == 0)
{
cp.add(new DriveTree(new File(".")));
}
else
{
cp.setLayout(new BoxLayout(cp, BoxLayout.X_AXIS));
for (int i = 0; i < av.length; i++)
cp.add(new DriveTree(new File(av[i])));
}
frame.pack();
frame.setVisible(true);
frame.setSize(300,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}