1 ////////////////////////////////////////////////////////////////////////////////////////////
           2 //
           3 //  @name JFileChooserDemo.java
           4 //
           5 //  @discription 文件選擇器演示程序
           6 //
           7 //  @author hcm
           8 //
           9 //  @date 2006-12
          10 //
          11 ////////////////////////////////////////////////////////////////////////////////////////////
          12 
          13 import java.awt.*;
          14 import java.awt.event.*;
          15 import javax.swing.*;
          16 import java.io.*;
          17 
          18 //文件選擇器演示
          19 
          20 public class JFileChooserDemo extends JFrame {
          21     private JFileChooser chooser;  //文件選擇器
          22     private JButton button;  //選擇文件按鈕
          23     private JComboBox comboBox;  //用于設定文件對話框作用(打開還是保存文件)
          24     
          25     public JFileChooserDemo() {
          26         super("JFileChooser 演示");  //調用父類構造函數
          27         Container contentPane = getContentPane();  //得到容器
          28         contentPane.setLayout(new FlowLayout());  //設置布局管理器為Flowlayout
          29         chooser=new JFileChooser();  //初始化文件選擇器
          30         button = new JButton("選擇文件");  //初始化按鈕
          31         comboBox=new JComboBox();  //初始化組合框
          32         comboBox.addItem("打開");  //增加組合框列表內容
          33         comboBox.addItem("保存");
          34         contentPane.add(comboBox);  //增加組件到容器
          35         contentPane.add(button);
          36         
          37         button.addActionListener(new ActionListener() {  //按鈕事件處理
          38             public void actionPerformed(ActionEvent e) {
          39                 int state;  //文件選擇器返回狀態
          40                 //  chooser.removeChoosableFileFilter(chooser.getAcceptAllFileFilter());  //移去所有文件過濾器
          41                 // chooser.addChoosableFileFilter(new MyFileFilter("gif","圖像文件"));   //增加文件過濾器,接愛gif文件
          42                 
          43                 if (comboBox.getSelectedIndex()==0)  //組合框為"打開"
          44                     
          45                 {
          46                     System.out.println("~~~~~~~~~~~~~"+0);
          47                     state=chooser.showOpenDialog(null);   //顯示打開文件對話框
          48                     System.out.println("zhuangtai===="+state);
          49                 } else {
          50                     System.out.println("~~~~~~~~~~~~~"+1);
          51                     state=chooser.showSaveDialog(null);  //顯示保存文件對話框
          52                 }
          53                 
          54                 File file = chooser.getSelectedFile();  //得到選擇的文件
          55                 if(true == ( file != null) ) {
          56                     System.out.println(file.toString());
          57                 }
          58                 if(file != null && state == JFileChooser.APPROVE_OPTION) {  //選擇了文件并點擊了打開可保存按鈕
          59                     JOptionPane.showMessageDialog(null, file.getPath()); //顯示提示信息
          60                 } else if(state == JFileChooser.CANCEL_OPTION) {  //點擊了撤銷按鈕
          61                     JOptionPane.showMessageDialog(null"退出!");  //顯示提示信息
          62                 } else if(state == JFileChooser.ERROR_OPTION) {
          63                     JOptionPane.showMessageDialog(null"錯誤!");  //顯示提示信息
          64                 }
          65             }
          66         });
          67         
          68         this.setSize(200,100);  //設置窗口大小
          69         this.setVisible(true);  //設置窗口可見
          70         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //關閉窗口時退出程序
          71     }
          72     
          73     public static void main(String args[]) {
          74         new JFileChooserDemo();
          75     }
          76 }
          77 

          再加個過濾器:
           1 import java.io.File;
           2 import javax.swing.filechooser.FileFilter;
           3 
           4 //文件過濾器
           5 
           6 public class MyFileFilter extends FileFilter
           7 {
           8     
           9     String ends;  //文件后綴
          10     String description;  //文件描述文字
          11     
          12     public MyFileFilter (String ends, String description)
          13     { //構造函數
          14         this.ends = ends;  //設置文件后綴
          15         this.description=description;  //設置文件描述文字
          16     }
          17     
          18     public boolean accept (File file)
          19     {  //重載FileFilter中的accept方法
          20         if (file.isDirectory ())  //如果是目錄,則返回true
          21             return true;
          22         String fileName = file.getName ();  //得到文件名稱
          23         if (fileName.toUpperCase ().endsWith (ends.toUpperCase ()))  //把文件后綴與可接受后綴轉成大寫后比較
          24             return true;
          25         else
          26             return false;
          27     }
          28     
          29     public String getDescription ()
          30     {  //返回文件描述文字
          31         return description;
          32     }
          33 }
          posted on 2007-02-06 16:18 -274°C 閱讀(1041) 評論(0)  編輯  收藏 所屬分類: JAVA

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網站

          關注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 914066
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 揭东县| 林口县| 简阳市| 隆尧县| 淮阳县| 宜兴市| 寿阳县| 石林| 治县。| 永丰县| 永德县| 高碑店市| 鄢陵县| 曲麻莱县| 扎鲁特旗| 湖南省| 琼海市| 清远市| 沅陵县| 玉溪市| 临武县| 南昌市| 屯留县| 托克逊县| 太白县| 马边| 永平县| 永丰县| 克拉玛依市| 砀山县| 博客| 彭州市| 海门市| 绵竹市| 尚志市| 黄梅县| 丹凤县| 闵行区| 韶山市| 布拖县| 大姚县|