日期:2014-05-20 浏览次数:20653 次
package cn.mrpmh.bao; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.text.SimpleDateFormat; import java.util.Date; //文件管理 public class jtable { JTable table; String[] columnName; JLabel xinxi; File[] fileslist; int photo=0,audio=0,video=0,txt=0; JTextField lujin; String lujinvalue="C:\\Users\\Administrator\\Pictures"; JButton go; JButton open; JFrame win; JScrollPane scroll; public void window(String title) { win=new JFrame(title); win.setIconImage(Toolkit.getDefaultToolkit().createImage("file/image/icon.png")); tool(); init(); win.setSize(600,400); win.setLocation(300,100); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); win.setVisible(true); } private void tool() { JToolBar tools=new JToolBar(); lujin=new JTextField(lujinvalue,15); go=new JButton("转到"); open=new JButton("打开"); tools.add(lujin); tools.add(go); tools.add(open); go.addActionListener(new Shijian()); open.addActionListener(new Shijian()); win.getContentPane().add(tools,BorderLayout.NORTH); } private String[][] files() { File file=new File(lujinvalue); fileslist=file.listFiles(); //获取文件数组 String[][] getfile=new String[fileslist.length][fileslist.length]; //定义字符串数组 for(int i=0;i<fileslist.length;i++) //遍历数组 { if(fileslist[i].getName().endsWith("jpg")||fileslist[i].getName().endsWith("gif")||fileslist[i].getName().endsWith("png")|| fileslist[i].getName().endsWith("bmp")) photo++; if(fileslist[i].getName().endsWith("mp3")||fileslist[i].getName().endsWith("wav")||fileslist[i].getName().endsWith("wma")|| fileslist[i].getName().endsWith("mid")) audio++; if(fileslist[i].getName().endsWith("mp4")||fileslist[i].getName().endsWith("3gp")||fileslist[i].getName().endsWith("avi")|| fileslist[i].getName().endsWith("flv")||fileslist[i].getName().endsWith("f4v")||fileslist[i].getName().endsWith("rm")|| fileslist[i].getName().endsWith("rmvb")||fileslist[i].getName().endsWith("wmv")) video++; if(fileslist[i].getName().endsWith("txt")) txt++; for(int j=0;j<4;j++) { getfile[i][0]=fileslist[i].getName(); //文件名 getfile[i][1]=fileslist[i].toString(); //路径 getfile[i][2]=Integer.toString(((int)fileslist[i].length())); //文件大小 getfile[i][3]=(new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss")).format(new Date( fileslist[i].lastModified())); } } return getfile; } private void init() { columnName=new String[]{"文件名","路径","大小(B)","最后修改时间"}; table=new JTable(files(),columnName); scroll=new JScrollPane(table); win.getContentPane().add(scroll,BorderLayout.CENTER); xinxi=new JLabel("本目录共【"+fileslist.length+"】个文件: 图片【"+photo+"】,音频【"+audio+"】,视频【"+video+"】,txt文本【"+txt+"】"); xinxi.setFont(new Font("",Font.PLAIN,12)); win.getContentPane().add(xinxi,BorderLayout.SOUTH); } public class Shijian implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()==go) { File file=new File(lujin.getText()); if(file.exists()) { lujinvalue=lujin.getText(); new Thread(new shuaxin()).start(); } else JOptionPane.showMessageDialog(null,"路径不存在!","提示",0); } if(e.getSource()==open) { FileDialog openfile=new FileDialog(new JFrame(),"打开",FileDialog.LOAD); openfile.setVisible(true); if(openfile.getFile()!=null) { lujinvalue=openfile.getDirectory();//+openfile.getFile(); lujin.setText(lujinvalue); } } } } class shuaxin implements Runnable { public void run() { table=new JTable(files(),columnName); scroll=new JScrollPane(table); } } public static void main(String[] args) { new jtable().window("文件管理器"); } }