|
![]() |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
package com.swing.tree; import java.io.*; import java.util.*; import javax.swing.tree.*; public class file_path_obj { private String dir_tree=""; private File temp; DefaultMutableTreeNode root=null; public DefaultMutableTreeNode getPath_Obj() { if(this.root==null) return new DefaultMutableTreeNode("null NODE"); return this.root; } public file_path_obj(String path) { try{ root= this.set(path); }catch(Exception e) { System.out.println("tree error: +++++++++++++++++++++++++++++++++"); e.printStackTrace(); }} public DefaultMutableTreeNode set(String dir) throws Exception //第归遍历文件甲 { File[] temp_list = null; if(new File(dir).exists()==false) throw new Exception("file name is null"); DefaultMutableTreeNode tempNode; temp = new File(dir); if(temp.isFile()) { return new DefaultMutableTreeNode(temp.getAbsolutePath()); } temp_list = temp.listFiles(); tempNode = new DefaultMutableTreeNode(temp.getAbsolutePath()); //设置上级菜单的名字 if(temp_list.length==0) return new DefaultMutableTreeNode(temp.getAbsolutePath()); for(int i=0;i<temp_list.length;i++) { if(temp_list[i].isDirectory()) { System.out.println("the file is:"+temp_list[i].getAbsolutePath()); tempNode.add(this.set(temp_list[i].getAbsolutePath())); } else tempNode.add(new DefaultMutableTreeNode(temp_list[i].getAbsolutePath())); } return tempNode; } } //------------------------------------------------------------ // 编译下边的类,并运行无需任何参数 //----------------------------------------------------------- package com.tree; import java.io.*; import java.awt.*; import javax.swing.*; import javax.swing.tree.*; import com.swing.tree.*; /** Example tree built out of DefaultMutableTreeNodes. * ycj */ public class pathTree extends JFrame { public static void main(String[] args) { try { BufferedReader bin = null; bin = new java.io.BufferedReader(new InputStreamReader(System.in)); String path=""; File temp = null; System.out.print("输入要显示的文件路径"); while(bin!=null) { path = bin.readLine(); if(path==null) { System.out.println("请输入合法的文件路径"); continue; } temp = new File(path); if(temp.exists()==false||temp.isFile()) System.out.println("请输入准确的文件路径"); else {new pathTree(path); break; } } }catch(Exception e) { e.printStackTrace(); } } public pathTree(String path) { super("Creating a Simple JTree"); WindowUtilities.setNativeLookAndFeel();//set the UIManager addWindowListener(new ExitListener()); Container content = getContentPane(); file_path_obj obj = new file_path_obj(path); System.out.println(obj.getPath_Obj()); JTree tree = new JTree(obj.getPath_Obj());//MAKE A TREE Cursor cor = new Cursor(Cursor.HAND_CURSOR); tree.setCursor(cor); content.setLayout(new FlowLayout()); JPanel jp1 = new JPanel(new BorderLayout()); JPanel jp2 = new JPanel(new BorderLayout()); content.add(jp1); content.add(jp2); content.add(new JScrollPane(tree), BorderLayout.CENTER); setSize(275, 500); setVisible(true); this.pack(); } } 返回类别: 教程 上一教程: 初学JAVA注重什么 下一教程: 用JAVAMAIL显示复合邮件的内容(1) 您可以阅读与"JAVA 写的文件浏览器"相关的教程: · Java的文件 读和写 · Java的文件操作--1 · 将 Java 小程序迁移到 Microsoft J# 浏览器控件 · java图像浏览器 · 通过java提供的URL类包读取网上的文件 |
![]() ![]() |
快精灵印艺坊 版权所有 |
首页![]() ![]() ![]() ![]() ![]() ![]() ![]() |