浏览 2324 次
锁定老帖子 主题:window系统资源管理树
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-11-29
最后修改:2008-11-29
昨天叫帮实现一个资源树,觉得挺有意思的,也顺便玩一下swing,学java到现在没玩过,惭愧。打开本core java ,翻了几页,于是就开干了.. package tree; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTree; public class Myframe extends JFrame { private JScrollPane js ; private JTree jt; private TreeNode mt; public Myframe() { initFrame(); } /** * 初始化用的... * */ public void initFrame() { super.setSize(200, 600); super.setDefaultCloseOperation(EXIT_ON_CLOSE); this.js = new JScrollPane(); this.mt = new TreeNode(); this.jt = new JTree(this.mt.createRootNode(),true); TreeAction ta = new TreeAction(this.jt); this.mt.setJt(this.jt); ta.setMt(this.mt); this.jt.addTreeExpansionListener(ta); this.js.setViewportView(this.jt); this.add(this.js); } public JScrollPane getJs() { return js; } public void setJs(JScrollPane js) { this.js = js; } public JTree getJt() { return jt; } public void setJt(JTree jt) { this.jt = jt; } public TreeNode getMt() { return mt; } public void setMt(TreeNode mt) { this.mt = mt; } } 一下是事件监听器类 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package tree; import java.io.File; import javax.swing.JTree; import javax.swing.event.TreeExpansionEvent; import javax.swing.event.TreeExpansionListener; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreePath; /** * * @author chen */ public class TreeAction implements TreeExpansionListener { private TreeNode mt ; private JTree jt; public TreeAction(JTree jtree){ this.jt = jtree; } //当前被选择的路径 private TreePath selectPath; //当前被选择的节点 private DefaultMutableTreeNode selectNode; public void treeExpanded(TreeExpansionEvent event) { //获取当前选择的节点对象 this.selectNode = (DefaultMutableTreeNode) event.getPath().getLastPathComponent(); //如果是根节点,则直接返回 if(this.selectNode.isRoot()){ return; } //回去节点路径 this.selectPath = event.getPath(); String path = event.getPath().toString(); //把节点路径转为对应的系统文件的绝对路径 path = Tool.reperse(path); if(this.jt.isExpanded(this.selectPath)){ this.mt.addTreeNode(this.selectNode,new File(path)); } } public void treeCollapsed(TreeExpansionEvent event) { //获取当前选择的节点对象 this.selectNode = (DefaultMutableTreeNode) event.getPath().getLastPathComponent(); if(this.selectNode.isRoot()){ return; } //删除当前节点下的所有子节点, this.selectNode.removeAllChildren(); this.selectPath = event.getPath(); if(!jt.isExpanded(this.selectPath)){ //用没有子节点的selectNode更新当前选择的节点 ((DefaultTreeModel)jt.getModel()).reload(this.selectNode); } } public JTree getJt() { return jt; } public void setJt(JTree jt) { this.jt = jt; } public TreeNode getMt() { return mt; } public void setMt(TreeNode mt) { this.mt = mt; } } 这个类是用来设置节点的 package tree; import java.io.File; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; public class TreeNode { private String ROOT_NAME = "我的电脑"; private JTree jt; private DefaultMutableTreeNode treeNode; /** * 根据系统的盘符数来初始化树根。 * @return */ public DefaultMutableTreeNode createRootNode(){ File dir = new File("."); DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(ROOT_NAME); for(int i = 0; i < dir.listRoots().length; i++){ if(dir.listRoots()[i].isDirectory()){ String rootPath = dir.listRoots()[i].getPath(); this.treeNode = new DefaultMutableTreeNode(rootPath); rootNode.add(this.treeNode); this.treeNode = null; } } return rootNode; } /** * 根据当前节点所对应目录下的所有子目录,创建对应的子节点 * @param node * @param dir * @return */ public void addTreeNode(DefaultMutableTreeNode node,File dir){ if(node == null || dir == null)return; if(!dir.isDirectory())return; if(!node.isRoot()){ //获取当前目录下的所有文件的绝对路径 File file[] = dir.listFiles(); for(int i = 0; i < file.length; i ++){ //只要文件夹,并且不能是隐藏文件夹 if(file[i].isDirectory() && !file[i].isHidden()){ //为文件夹创建对应的子 this.treeNode = new DefaultMutableTreeNode(dir.list()[i]); //把子节点加载到当前所选择的节点 ((DefaultTreeModel)this.jt.getModel()).insertNodeInto(treeNode, node, node.getChildCount()); this.treeNode = null; } } } } public JTree getJt() { return jt; } public void setJt(JTree jt) { this.jt = jt; } public DefaultMutableTreeNode getTreeNode() { return treeNode; } public void setTreeNode(DefaultMutableTreeNode treeNode) { this.treeNode = treeNode; } } 这个不知道怎么描述,先放着吧 package tree; /** * * @author chen */ public class Tool { public static String reperse(String str){ str = str.replace("[我的电脑, ",""); str = str.replace("]",""); str = str.replace(", ","\\"); str = str.replace("\\\\","\\"); return str; } } 这个是测试的 package tree; import java.io.File; public class MyMain { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Myframe mf = new Myframe(); mf.setVisible(true); } } 这里只显示目录, 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |