importjava.io.*;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importjavax.swing.table.DefaultTableModel;importjava.lang.Runtime;importjava.lang.Process;importjava.util.Date;importjava.text.SimpleDateFormat;/*** 实现本地文件浏览,为继承JPanel的一个面板
*
*@authorLonsy
*@version1.0*/publicclassLocalFileextendsJPanelimplementsActionListener, MouseListener
{privateJButton jbUp;privateJComboBox jcbPath;privateJTable jtFile;privateDefaultTableModel dtmFile;privateJLabel jlLocal;privateFile path;privateString currentPath;privateintcurrentIndex;privatebooleaninit=false;publicLocalFile() {super(newBorderLayout());
JPanel jp=newJPanel(newBorderLayout());
jbUp=newJButton("Up");
jbUp.addActionListener(this);
jcbPath=newJComboBox();
jcbPath.addActionListener(this);
jp.add(jbUp,"West");
jp.add(jcbPath,"Center");
dtmFile=newLocalTableModel();
dtmFile.addColumn("名称");
dtmFile.addColumn("大小");
dtmFile.addColumn("类型");
dtmFile.addColumn("修改日期");
jtFile=newJTable(dtmFile);
jtFile.setShowGrid(false);
jtFile.addMouseListener(this);
jlLocal=newJLabel("本地状态", JLabel.CENTER);
add(jp,"North");
add(newJScrollPane(jtFile),"Center");
add(jlLocal,"South");//显示系统分区及文件路径 并 在JTabel中显示当前路径的文件信息path=newFile(System.getProperty("user.dir"));
listFiles(path);
init=true;
}//处理路径的选择事件publicvoidactionPerformed(ActionEvent e) {if(e.getSource()==jbUp&&jtFile.getValueAt(0,0).toString().equals("返回上级")&&jtFile.getValueAt(0,2).toString().equals(""))
{
listFiles(newFile(currentPath).getParentFile());return;
}if(init==false)
{return;
}intindex=jcbPath.getSelectedIndex();
String item=(String)jcbPath.getSelectedItem();if(item.startsWith(""))
{introot=index-1;while(((String)jcbPath.getItemAt(root)).startsWith(""))
{
root--;
}
String path=(String)jcbPath.getItemAt(root);while(root
{
path+=((String)jcbPath.getItemAt(++root)).trim();;
path+="//";
}if(listFiles(newFile(path))==false)
{
jcbPath.setSelectedIndex(currentIndex);
}else{
currentIndex=index;
}
}else{if(listFiles(newFile(item))==false)
{
jcbPath.setSelectedIndex(currentIndex);
}else{
currentIndex=index;
}
}
}//JTable里文件夹双击事件publicvoidmouseClicked(MouseEvent e) {if(e.getClickCount()==2) {introw=((JTable)e.getSource()).getSelectedRow();if(((JTable)e.getSource()).getValueAt(row,2).toString().equals("文件夹"))
{
File file;//判断是否为根目录,作不同处理。一个 / 的差别if(currentPath.split("").length>1)
{
file=newFile(currentPath+"//"+((JTable)e.getSource()).getValueAt(row,0).toString());
}else{
file=newFile(currentPath+((JTable)e.getSource()).getValueAt(row,0).toString());
}
listFiles(file);
}elseif(((JTable)e.getSource()).getValueAt(row,0).toString().equals("返回上级")&&((JTable)e.getSource()).getValueAt(row,2).toString().equals(""))
{
listFiles(newFile(currentPath).getParentFile());
}
}
}//其他一堆无用的事件publicvoidmouseEntered(MouseEvent e) {}publicvoidmouseExited(MouseEvent e) {}publicvoidmousePressed(MouseEvent e) {}publicvoidmouseReleased(MouseEvent e) {}//显示系统分区及文件路径 并 在JTabel中显示当前路径的文件信息privatebooleanlistFiles(File path) {
String strPath=path.getAbsolutePath();if(path.isDirectory()==false)
{
JOptionPane.showMessageDialog(this,"此路径不存在,或无此文件");returnfalse;
}
currentPath=path.getAbsolutePath();
init=false;
jcbPath.removeAllItems();
File[] roots=File.listRoots();intindex=0;for(inti=0; i
{
String rootPath=roots[i].getAbsolutePath();
jcbPath.addItem(rootPath);if(currentPath.indexOf(rootPath)!=-1)
{
String[] bufPath=currentPath.split("");for(intj=1; j
{
String buf="";for(intk=1; k
{
buf+="";
}
jcbPath.addItem(buf+bufPath[j]);
index=i+j;
}if(bufPath.length==1)
{
index=i;
}
}
}
jcbPath.setSelectedIndex(index);
init=true;
currentIndex=index;//清空现有数据dtmFile.setRowCount(0);//如果判断为非分区根目录,则添加 返回上级 一行if(strPath.split("").length>1)
{
dtmFile.addRow(newString[]{"返回上级","","",""});
}//列出当前目录所有目录及文件File[] files=path.listFiles();for(inti=0; i
{
String name=files[i].getName();if(files[i].isDirectory())
{
dtmFile.addRow(newString[]{name,"","文件夹",""});
}else{if(name.lastIndexOf(".")!=-1)
{
dtmFile.addRow(newString[]{name.substring(0, name.lastIndexOf(".")),
sizeFormat(files[i].length()),
name.substring(name.lastIndexOf(".")+1),newSimpleDateFormat("yyyy-MM-dd hh:mm").format(newDate(files[i].lastModified()))});
}else{
dtmFile.addRow(newString[]{name,
sizeFormat(files[i].length()),"",newSimpleDateFormat("yyyy-MM-dd hh:mm").format(newDate(files[i].lastModified()))});
}
}
}
jlLocal.setText(currentPath);returntrue;
}//将文件大小转换成相应字符串格式privateString sizeFormat(longlength) {longkb;if(length<1024)
{returnString.valueOf(length);
}elseif((kb=length/1024)<1024)
{return(String.valueOf(kb)+"kb");
}else{return(String.valueOf(length/1024/1024)+"kb");
}
}//测试publicstaticvoidmain(String[] args) {
JFrame jf=newJFrame("测试");
jf.setSize(300,400);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension di=Toolkit.getDefaultToolkit().getScreenSize();
jf.setLocation((int)(di.getWidth()-jf.getWidth())/2,
(int)(di.getHeight()-jf.getHeight())/2);
jf.add(newLocalFile());
jf.setVisible(true);
}//实现相应的tablemodel类classLocalTableModelextendsDefaultTableModel
{publicbooleanisCellEditable(introw,intcolumn) {returnfalse;
}
}
}