march alex's blog
          hello,I am march alex
          posts - 52,comments - 7,trackbacks - 0
          JFileChooser() 
                    構造一個指向用戶默認目錄的 JFileChooser
          JFileChooser(File currentDirectory)
                    使用給定的 File 作為路徑來構造一個 JFileChooser

          setFileSelectionMode(int mode)
                    設置 JFileChooser,以允許用戶只選擇文件、只選擇目錄,或者可選擇文件和目錄。

          mode參數:FILES_AND_DIRECTORIES   指示顯示文件和目錄。

                FILES_ONLY                             指示僅顯示文件。

               DIRECTORIES_ONLY                指示僅顯示目錄。

          showDialog(Component parent,String approveButtonText)
                    彈出具有自定義 approve 按鈕的自定義文件選擇器對話框。

          showOpenDialog(Component parent)
                    彈出一個 "Open File" 文件選擇器對話框。

          showSaveDialog(Component parent)
                    彈出一個 "Save File" 文件選擇器對話框。

          setMultiSelectionEnabled(boolean b)
                    設置文件選擇器,以允許選擇多個文件。

          getSelectedFiles() 
                    如果將文件選擇器設置為允許選擇多個文件,則返回選中文件的列表(File[])。

          getSelectedFile()
                    返回選中的文件。

          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;
          import java.io.File;

          import javax.swing.JButton;
          import javax.swing.JFileChooser;
          import javax.swing.JFrame;
          import javax.swing.JLabel;

          public class FileChooser extends JFrame implements ActionListener{
              JButton open=null;
              public static void main(String[] args) {
                  new FileChooser();
              }
              public FileChooser(){
                  open=new JButton("open");
                  this.add(open);
                  this.setBounds(400, 200, 100, 100);
                  this.setVisible(true);
                  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  open.addActionListener(this);
              }
              @Override
              public void actionPerformed(ActionEvent e) {
                  // TODO Auto-generated method stub
                  JFileChooser jfc=new JFileChooser();
                  jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
                  jfc.showDialog(new JLabel(), "選擇");
                  File file=jfc.getSelectedFile();
                  if(file.isDirectory()){
                      System.out.println("文件夾:"+file.getAbsolutePath());
                  }else if(file.isFile()){
                      System.out.println("文件:"+file.getAbsolutePath());
                  }
                  System.out.println(jfc.getSelectedFile().getName());
                  
              }

          }
          posted on 2015-03-18 12:35 marchalex 閱讀(570) 評論(0)  編輯  收藏 所屬分類: java小程序
          主站蜘蛛池模板: 东港市| 腾冲县| 武定县| 同德县| 邮箱| 锦屏县| 石渠县| 台安县| 惠来县| 延吉市| 剑川县| 绥江县| 北川| 辽宁省| 江源县| 获嘉县| 都匀市| 会同县| 固始县| 双峰县| 长丰县| 玉屏| 福泉市| 嵊泗县| 天津市| 双柏县| 樟树市| 称多县| 平度市| 米泉市| 南丹县| 手机| 龙游县| 边坝县| 松桃| 苏尼特右旗| 西藏| 乌海市| 夹江县| 亚东县| 册亨县|