1、使用File类的递归搜索方法实现
import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;
import java.io.File;
public class Exec
{
public static void main(String[] args)
{
MyFrame m = new MyFrame();
m.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Toolkit theKit = m.getToolkit();
Dimension wndSize = theKit.getScreenSize();
m.setBounds(wndSize.width/4, wndSize.height/4, wndSize.width/2, wndSize.height/2);
m.setVisible(true);
}
}
class MyFrame extends JFrame
{
JTree tree = null;
public MyFrame()
{
setLocation(10, 100);
setSize(600, 400);
getContentPane().setLayout(new BorderLayout());
File f=new File("D:\\LSQ\\WebOthers\\tmp");
DefaultMutableTreeNode top = new DefaultMutableTreeNode(f.getName());
seeIt(top,f);
tree = new JTree(top);
getContentPane().add(tree, BorderLayout.CENTER);
}
public void seeIt(DefaultMutableTreeNode superDmtn,File f)
{
File files[] = f.listFiles();
for (int i = 0; i < files.length; i++)
{
DefaultMutableTreeNode dmtn = new DefaultMutableTreeNode(files[i].getName());
superDmtn.add(dmtn);
if (files[i].isDirectory())
{
seeIt(dmtn,files[i]);
}
}
}
}
2、添加滚动条
import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;
import java.io.File;
public class Exec
{
public static void main(String[] args)
{
MyFrame m = new MyFrame();
m.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Toolkit theKit = m.getToolkit();
Dimension wndSize = theKit.getScreenSize();
m.setBounds(wndSize.width/4, wndSize.height/4, wndSize.width/2, wndSize.height/2);
m.setVisible(true);
}
}
class MyFrame extends JFrame
{
JTree tree = null;
public MyFrame()
{
setLocation(10, 100);
setSize(600, 400);
getContentPane().setLayout(new BorderLayout());
File f=new File("D:\\LSQ\\WebOthers\\tmp");
DefaultMutableTreeNode top = new DefaultMutableTreeNode(f.getName());
seeIt(top,f);
tree = new JTree(top);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(tree, v, h);
getContentPane().add(jsp, BorderLayout.CENTER);
}
public void seeIt(DefaultMutableTreeNode superDmtn,File f)
{
File files[] = f.listFiles();
for (int i = 0; i < files.length; i++)
{
DefaultMutableTreeNode dmtn = new DefaultMutableTreeNode(files[i].getName());
superDmtn.add(dmtn);
if (files[i].isDirectory())
{
seeIt(dmtn,files[i]);
}
}
}
}
3、基于事件处理和模型编程的高效及时遍历方式
import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;
import java.io.File;
import javax.swing.event.*;
public class Exec
{
public static void main(String[] args)
{
MyFrame m = new MyFrame();
m.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Toolkit theKit = m.getToolkit();
Dimension wndSize = theKit.getScreenSize();
m.setBounds(wndSize.width/4, wndSize.height/4, wndSize.width/2, wndSize.height/2);
m.setVisible(true);
}
}
class MyFrame extends JFrame implements TreeExpansionListener
{
JTree tree = null;
DefaultTreeModel dtm=null;
public MyFrame()
{
setLocation(10, 100);
setSize(600, 400);
getContentPane().setLayout(new BorderLayout());
File f=new File("c:\\");
DefaultMutableTreeNode top = new DefaultMutableTreeNode(f.getPath());
getRoot(top,f);
dtm=new DefaultTreeModel(top);
dtm.setAsksAllowsChildren(true);
tree = new JTree(dtm);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(tree, v, h);
getContentPane().add(jsp, BorderLayout.CENTER);
tree.addTreeExpansionListener(this);
}
public void getRoot(DefaultMutableTreeNode superDmtn,File f)
{
File files[] = f.listFiles();
for (int i = 0; i < files.length; i++)
{
DefaultMutableTreeNode dmtn = new DefaultMutableTreeNode(files[i].getName());
if(files[i].isDirectory())
dmtn.setAllowsChildren(true);
else
dmtn.setAllowsChildren(false);
superDmtn.add(dmtn);
}
}
public void treeExpanded(TreeExpansionEvent event)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)(event.getPath()).getLastPathComponent();
DefaultMutableTreeNode originalNode=node;
String fileName=node.toString();
while(node.getParent()!=null)
{
node=(DefaultMutableTreeNode)node.getParent();
fileName=node.toString()+"\\"+fileName;
}
if(originalNode!=null)
{
File f=new File(fileName);
if(f.isDirectory())
{
File files[] = f.listFiles();
for (int i = 0; i < files.length; i++)
{
DefaultMutableTreeNode dmtn = new DefaultMutableTreeNode(files[i].getName());
if(files[i].isDirectory())
dmtn.setAllowsChildren(true);
else
dmtn.setAllowsChildren(false);
dtm.insertNodeInto(dmtn,originalNode,originalNode.getChildCount());
TreeNode[] nodes=dtm.getPathToRoot(dmtn);
TreePath path=new TreePath(nodes);
tree.makeVisible(path);
}
}
}
}
public void treeCollapsed(TreeExpansionEvent event)
{
}
}
[此贴子已经被作者于2010-12-12 08:18:05编辑过]