隨筆-8  評論-14  文章-0  trackbacks-0
          一個項目需要,籍此做的一個瀏覽器,測試結果如下:



          下面是其源碼,歡迎拍磚:

          import java.io.*;
          import javax.swing.*;
          import java.awt.*;
          import java.awt.event.*;
          import javax.swing.table.DefaultTableModel;
          import java.lang.Runtime;
          import java.lang.Process;
          import java.util.Date;
          import java.text.SimpleDateFormat;

          /**
           * 實現本地文件瀏覽,為繼承JPanel的一個面板
           *
           * 
          @author Lonsy
           * 
          @version 1.0
           
          */
          public class LocalFile extends JPanel implements ActionListener, MouseListener
          {
              
          private JButton jbUp;
              
          private JComboBox jcbPath;
              
          private JTable jtFile;
              
          private DefaultTableModel dtmFile;
              
          private JLabel jlLocal;
              
          private File path;
              
          private String currentPath;
              
          private int currentIndex;
              
          private boolean init = false;

              
          public LocalFile() {
                  
          super(new BorderLayout());
                  
                  JPanel jp 
          = new JPanel(new BorderLayout());
                  jbUp 
          = new JButton("Up");
                  jbUp.addActionListener(
          this);
                  jcbPath 
          = new JComboBox();
                  jcbPath.addActionListener(
          this);
                  jp.add(jbUp, 
          "West");
                  jp.add(jcbPath, 
          "Center");
                  dtmFile 
          = new LocalTableModel();
                  dtmFile.addColumn(
          "名稱");
                  dtmFile.addColumn(
          "大小");
                  dtmFile.addColumn(
          "類型");
                  dtmFile.addColumn(
          "修改日期");
                  jtFile 
          = new JTable(dtmFile);
                  jtFile.setShowGrid(
          false);
                  jtFile.addMouseListener(
          this);
                  jlLocal 
          = new JLabel("本地狀態", JLabel.CENTER);

                  add(jp, 
          "North");
                  add(
          new JScrollPane(jtFile), "Center");
                  add(jlLocal, 
          "South");

                  
          //顯示系統分區及文件路徑 并 在JTabel中顯示當前路徑的文件信息
                  path = new File(System.getProperty("user.dir"));
                  listFiles(path);    

                  init 
          = true;
              }

              
          //處理路徑的選擇事件
              public void actionPerformed(ActionEvent e) {
                  
          if (e.getSource()==jbUp && jtFile.getValueAt(00).toString().equals("返回上級")
                          
          && jtFile.getValueAt(02).toString().equals(""))
                  {
                      listFiles(
          new File(currentPath).getParentFile());
                      
          return;
                  }
                  
          if (init == false)
                  {
                      
          return;
                  }
                  
          int index = jcbPath.getSelectedIndex();
                  String item 
          = (String)jcbPath.getSelectedItem();
                  
          if (item.startsWith("  "))
                  {
                      
          int root = index - 1;
                      
          while (((String)jcbPath.getItemAt(root)).startsWith("  "))
                      {
                          root
          --;
                      }
                      String path 
          = (String)jcbPath.getItemAt(root);
                      
          while (root < index)
                      {
                          path 
          += ((String)jcbPath.getItemAt(++root)).trim();;
                          path 
          += "\\";
                      }
                      
          if (listFiles(new File(path)) == false)
                      {
                          jcbPath.setSelectedIndex(currentIndex);
                      }
                      
          else
                      {
                          currentIndex 
          = index;
                      }
                  }
                  
          else
                  {
                      
          if (listFiles(new File(item)) == false)
                      {
                          jcbPath.setSelectedIndex(currentIndex);
                      }
                      
          else
                      {
                          currentIndex 
          = index;
                      }
                  }
              }

              
          //JTable里文件夾雙擊事件
              public void mouseClicked(MouseEvent e) {
                  
          if(e.getClickCount()==2) {
                      
          int row = ((JTable)e.getSource()).getSelectedRow();
                      
          if (((JTable)e.getSource()).getValueAt(row, 2).toString().equals("文件夾"))
                      {
                          File file;
                          
          //判斷是否為根目錄,作不同處理。一個 \ 的差別
                          if (currentPath.split("\\\\").length > 1)
                          {
                              file 
          = new File(currentPath + "\\" + ((JTable)e.getSource()).getValueAt(row, 0).toString());
                          }
                          
          else
                          {                    
                              file 
          = new File(currentPath + ((JTable)e.getSource()).getValueAt(row, 0).toString());
                          }
                          listFiles(file);
                      }
                      
          else if (((JTable)e.getSource()).getValueAt(row, 0).toString().equals("返回上級")
                              
          && ((JTable)e.getSource()).getValueAt(row, 2).toString().equals(""))
                      {
                          listFiles(
          new File(currentPath).getParentFile());
                      }
                  }
              }
              
          //其他一堆無用的事件
              public void mouseEntered(MouseEvent e) {}
              
          public void mouseExited(MouseEvent e) {}
              
          public void mousePressed(MouseEvent e) {}
              
          public void mouseReleased(MouseEvent e) {}

              
          //顯示系統分區及文件路徑 并 在JTabel中顯示當前路徑的文件信息
              private boolean listFiles(File path) {
                  String strPath 
          = path.getAbsolutePath();
                  
          if (path.isDirectory() == false)
                  {
                      JOptionPane.showMessageDialog(
          this"此路徑不存在,或無此文件");
                      
          return false;
                  }
                  
                  currentPath 
          = path.getAbsolutePath();
                  init 
          = false;
                  jcbPath.removeAllItems();
                  File[] roots 
          = File.listRoots();
                  
          int index = 0;
                  
          for (int i=0; i<roots.length; i++)
                  {
                      String rootPath 
          = roots[i].getAbsolutePath();
                      jcbPath.addItem(rootPath);
                      
          if (currentPath.indexOf(rootPath) != -1)
                      {
                          String[] bufPath 
          = currentPath.split("\\\\");
                          
          for (int j=1; j<bufPath.length; j++)
                          {
                              String buf 
          = "  ";
                              
          for (int k=1; k<j; 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(
          new String[]{"返回上級"""""""});
                  }

                  
          //列出當前目錄所有目錄及文件
                  File[] files = path.listFiles();
                  
          for (int i=0; i<files.length; i++)
                  {
                      String name 
          = files[i].getName();
                      
          if (files[i].isDirectory())
                      {
                          dtmFile.addRow(
          new String[]{name, """文件夾"""});
                      }
                      
          else
                      {
                          
          if (name.lastIndexOf("."!= -1)
                          {
                              dtmFile.addRow(
          new String[]{name.substring(0, name.lastIndexOf(".")), 
                                      sizeFormat(files[i].length()), 
                                      name.substring(name.lastIndexOf(
          "."+ 1),
                                      
          new SimpleDateFormat("yyyy-MM-dd hh:mm").format(new Date(files[i].lastModified()))});
                          }
                          
          else
                          {
                              dtmFile.addRow(
          new String[]{name, 
                                      sizeFormat(files[i].length()), 
                                      
          "",
                                      
          new SimpleDateFormat("yyyy-MM-dd hh:mm").format(new Date(files[i].lastModified()))});
                          }
                      }
                  }
                  
                  jlLocal.setText(currentPath);

                  
          return true;
              }

              
          //將文件大小轉換成相應字符串格式
              private String sizeFormat(long length) {
                  
          long kb;
                  
          if (length < 1024)
                  {
                      
          return String.valueOf(length);
                  }
                  
          else if ((kb = length / 1024< 1024)
                  {
                      
          return (String.valueOf(kb) + "kb");
                  }
                  
          else
                  {
                      
          return (String.valueOf(length / 1024 / 1024+ "kb");
                  }
              }

              
          //測試
              public static void main(String[] args) {
                  JFrame jf 
          = new JFrame("測試");
                  jf.setSize(
          300400);
                  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(
          new LocalFile());
                  jf.setVisible(
          true);
              }

              
          //實現相應的tablemodel類
              class LocalTableModel extends DefaultTableModel
              {
                  
          public boolean isCellEditable(int row, int column) {
                      
          return false;
                  }  
              }
          }

          posted on 2008-08-05 01:05 Lonsy 閱讀(2929) 評論(1)  編輯  收藏 所屬分類: BearFTP

          評論:
          # re: 用Java實現的一個本地文件瀏覽器 2009-03-06 11:04 | bijat
          正好需要呢,
          我就不用自己寫了
          謝謝了  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 三穗县| 巴中市| 余姚市| 大渡口区| 云安县| 阜阳市| 新田县| 南和县| 汝州市| 独山县| 石景山区| 清原| 时尚| 汽车| 玛多县| 哈巴河县| 开化县| 古交市| 昭觉县| 宁都县| 乌拉特中旗| 东明县| 虎林市| 芷江| 阳新县| 天台县| 柳林县| 丹寨县| 启东市| 错那县| 恩施市| 大化| 桦川县| 修水县| 鄯善县| 宜黄县| 湘潭市| 长宁县| 千阳县| 阿克陶县| 中超|