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 閱讀(563) 評論(0)  編輯  收藏 所屬分類: java小程序
          主站蜘蛛池模板: 特克斯县| 永年县| 灵石县| 壶关县| 罗平县| 察哈| 成武县| 山阳县| 界首市| 永宁县| 南澳县| 泸定县| 苏尼特左旗| 屏边| 莫力| 大兴区| 黄浦区| 万全县| 诏安县| 乌兰察布市| 全椒县| 襄城县| 巴彦县| 永平县| 文昌市| 山阴县| 个旧市| 磐石市| 云安县| 廉江市| 凌云县| 阿坝县| 鹤岗市| 宁津县| 乐业县| 年辖:市辖区| 金堂县| 勐海县| 霍山县| 自贡市| 开江县|