课外天地 李树青学习天地Java程序语言课件 → 程序代码——使用JTree组件实现的文件资源管理器


  共有20051人关注过本帖树形打印复制链接

主题:程序代码——使用JTree组件实现的文件资源管理器

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1945 积分:26736 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——使用JTree组件实现的文件资源管理器  发帖心情 Post By:2007/5/30 11:10:19 [显示全部帖子]

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编辑过]

 回到顶部