java写的文件浏览器[Java编程]
本文“java写的文件浏览器[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
//------------------------------------------------------------
// 编译下边的类,并运行无需任何参数
//-----------------------------------------------------------
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();
}
}
以上是“java写的文件浏览器[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |
- ·上一篇文章:关于java vm的垃圾堆积
- ·下一篇文章:提高EJB性能的本领
- ·中查找“java写的文件浏览器”更多相关内容
- ·中查找“java写的文件浏览器”更多相关内容