java写的文件浏览器
时间:2011-01-18
//------------------------------------------------------------
// 编译下边的类,并运行无需任何参数
//-----------------------------------------------------------
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:\n+++++++++++++++++++++++++++++++++");
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();
}
}
|