几天的努力,辛辛苦苦的总算把这个资源管理器给做了出来,总体上讲目前虽然实现了管理器功能,但是代码显得很混乱,并未实现界面与数据的分离,界面设计,监听Listener,数据处理的所有代码只是写在一个类中.
现代编程思想把程序分成界面展示层、业务罗基层和数据访问层。界面展示层负责和用户交互,界面层的输入或者查询请求首先告诉业务逻辑层,业务逻辑模块根据分析业务、查找缓存等等操作决定是否访问数据库,访问数据库调用什么存储过程或者直接执行sql语句,然后通知数据访问层。数据访问层建立连接池根据业务层的请求访问数据库,并将结果返回。即实现低耦合.也就是说处理不同逻辑的代码应该放在不同的地方.
这样可以是程序结构更加清晰,实现底层透明.更易与维护与优化.如果时间允许,我会分离出这个程序.现在我先把目前我做的的资源管理器代码列示一下,供大家参考:
package tabletest;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Date;
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
public class Test1 extends DefaultMutableTreeNode {
private static final long serialVersionUID = 6999685634761062308L;
private class MyFile {//这个内部类的用处在于在JTree节点的select发生时,对所选文件进行格式转化
private File file;
public MyFile(File file) {
this.file = file;
}
public String toString() {
String name = file.getName().trim();
if (name.length() == 0)
name = file.getAbsolutePath().trim();
return name;
}
}
DefaultMutableTreeNode treeRoot;
DefaultTableModel tableModel;
JTable table;
DefaultMutableTreeNode parent;
Object[][] list = { {} };
public void treeMake() throws UnknownHostException { // 添加机器码&&构造界面
InetAddress local = InetAddress.getLocalHost();
treeRoot = new DefaultMutableTreeNode(local.getHostName());
final JTree tree = new JTree(treeRoot);
JScrollPane scrolltree = new JScrollPane(tree);
scrolltree.setPreferredSize(new Dimension(200, 300));
String[] row = { "文件", "类型", "最后修改日期" };
tableModel = new DefaultTableModel(list, row);
table = new JTable(tableModel);
JScrollPane scrollTable = new JScrollPane(table);
scrollTable.setPreferredSize(new Dimension(500, 500));
JFrame jf = new JFrame();
jf.add(BorderLayout.WEST, scrolltree);
jf.add(BorderLayout.CENTER, scrollTable);
jf.setSize(600, 600);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 添加电脑本地硬盘
for (char i = 'c'; i < 'z'; i++) {
String path = i + ":/";
if (((new File(path)).exists())) {
DefaultMutableTreeNode diskNode = new DefaultMutableTreeNode(
new MyFile(new File(path)));
treeRoot.add(diskNode);
readfiles(new File(path), diskNode);
}
}
tree.addTreeSelectionListener(new TreeSelectionListener() {//添加listener
public void valueChanged(TreeSelectionEvent arg0) {
// TODO 自动生成方法存根
TreePath path = tree.getSelectionPath();
if (path == null)
return;
DefaultMutableTreeNode selectnode = (DefaultMutableTreeNode) path
.getLastPathComponent();
if (!selectnode.isLeaf()) {
// 这里加上类型判断
if (!(selectnode.getUserObject() instanceof MyFile)) {
return;
}
File file_select = ((MyFile) selectnode.getUserObject()).file;
// 读取文件夹下文件添加下层节点
readfiles(file_select, selectnode);
DefaultMutableTreeNode firstchild = (DefaultMutableTreeNode) selectnode
.getFirstChild();
selectnode.remove(firstchild);
tableModel.setRowCount(0);
File[] fileList = file_select.listFiles();
list = fu(fileList);
for (int i = 0; i < fileList.length; i++) {
tableModel.addRow(list[i]);
}
}
else {
tableModel.setRowCount(0);
try {
File file_select = ((MyFile) selectnode.getUserObject()).file;
String sizeFile=size(file_select);
Object re[][]={{file_select.getName(),sizeFile,lastTime(file_select)}};
list=re;
tableModel.addRow(list[0]);
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
});
}
public void readfiles(File file, DefaultMutableTreeNode node) {//读取所选节点,获取子节点
File list[] = file.listFiles();
if (list == null)
return;
for (int i = 0; i < list.length; i++) {
File file_inlist = list[i];
// String filename = file_inlist.getName();
if (file_inlist.isDirectory()) {
parent = new DefaultMutableTreeNode(new MyFile(file_inlist));
// 添加空白文件夹节点 使子节点显示为文件夹
File stubadd = null;
DefaultMutableTreeNode stub = new DefaultMutableTreeNode(
stubadd);
parent.add(stub);
node.add(parent);
} else {
DefaultMutableTreeNode son = new DefaultMutableTreeNode(
new MyFile(file_inlist));
node.add(son);
}
}
}
public String size(File file) throws IOException {//读取文件的大小
FileInputStream fileLength = new FileInputStream(file);
String sizefile = fileLength.available() + "字节";
return sizefile;
}
public Date lastTime(File file) {
long lastModified = file.lastModified();// 取得最后一次修改的时间
Date date = new Date(lastModified);
date.setTime(lastModified);
return date;
}
public Object[][] fu(File[] file) {
Object[][] m = new Object[file.length][3];
for (int i = 0; i < file.length; i++) {
m[i][0] = file[i].getName();
try {
// 这里有问题,如果是目录,怎么取大小?所以要用if
// m[i][1] = size(file[i]);
if (file[i].isDirectory()) {
m[i][1] = "目录";
} else {
m[i][1] = size(file[i]);
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
m[i][2] = lastTime(file[i]);
}
return m;
}
public int getColumnCount() {
// TODO 自动生成方法存根
return 3;
}
public int getRowCount(File[] file) {
// TODO 自动生成方法存根
return file.length;
}
public Object getValueAt(int row, int col) {
// TODO 自动生成方法存根
return list[row][col];
}
public static void main(String[] args) {
// TODO Auto-generated method
Test1 disk = new Test1();
try {
disk.treeMake();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
程序在我机器上已经运行过,所以代码应该没错,大家学习时候,只要注意类名和包不要弄错,就可以运行.