FileChooser的使用,并且可以进行类型的过滤

java 代码
  1. package example;  
  2.   
  3. import java.io.File;  
  4.   
  5. import javax.swing.filechooser.FileFilter;  
  6.   
  7. public class SimpleFileFilter extends FileFilter {  
  8.   
  9.     String[] extensions;  
  10.   
  11.     String description;  
  12.   
  13.     public SimpleFileFilter(String ext) {  
  14.         this(new String[] { ext }, null);  
  15.     }  
  16.   
  17.     public SimpleFileFilter(String[] exts, String descr) {  
  18.         // Clone and lowercase the extensions  
  19.         extensions = new String[exts.length];  
  20.         for (int i = exts.length - 1; i >= 0; i--) {  
  21.             extensions[i] = exts[i].toLowerCase();  
  22.         }  
  23.         // Make sure we have a valid (if simplistic) description.  
  24.         description = (descr == null ? exts[0] + " files" : descr);  
  25.     }  
  26.   
  27.     public boolean accept(File f) {  
  28.         // We always allow directories, regardless of their extensions.  
  29.         if (f.isDirectory()) {  
  30.             return true;  
  31.         }  
  32.   
  33.         // It's a regular file, so check the extension.  
  34.         String name = f.getName().toLowerCase();  
  35.         for (int i = extensions.length - 1; i >= 0; i--) {  
  36.             if (name.endsWith(extensions[i])) {  
  37.                 return true;  
  38.             }  
  39.         }  
  40.         return false;  
  41.     }  
  42.   
  43.     public String getDescription() {  
  44.         return description;  
  45.     }  
  46.   
  47. }  
java 代码
  1. package example;  
  2.   
  3. import java.awt.Container;  
  4. import java.awt.FlowLayout;  
  5. import java.awt.event.ActionEvent;  
  6. import java.awt.event.ActionListener;  
  7.   
  8. import javax.swing.JButton;  
  9. import javax.swing.JFileChooser;  
  10. import javax.swing.JFrame;  
  11. import javax.swing.JLabel;  
  12.   
  13. public class MyFilterChooser extends JFrame {  
  14.       public MyFilterChooser( ) {  
  15.         super("Filter Test Frame");  
  16.         setSize(350200);  
  17.         setDefaultCloseOperation(EXIT_ON_CLOSE);  
  18.   
  19.         Container c = getContentPane( );  
  20.         c.setLayout(new FlowLayout( ));  
  21.           
  22.         JButton openButton = new JButton("Open");  
  23.         final JLabel statusbar = new JLabel("Output of your selection will go here");  
  24.   
  25.         openButton.addActionListener(new ActionListener( ) {  
  26.           public void actionPerformed(ActionEvent ae) {  
  27.             String[] pics = new String[] {"gif""jpg""tif"};  
  28.             String[] audios = new String[] {"au""aiff""wav"};  
  29.             JFileChooser chooser = new JFileChooser( );  
  30.             chooser.addChoosableFileFilter(new SimpleFileFilter(pics,   
  31.                                            "Images (*.gif, *.jpg, *.tif)"));  
  32.             chooser.addChoosableFileFilter(new SimpleFileFilter(".MOV"));  
  33.             chooser.addChoosableFileFilter(new SimpleFileFilter(audios,   
  34.                                            "Sounds (*.aiff, *.au, *.wav)"));  
  35.             int option = chooser.showOpenDialog(MyFilterChooser.this);  
  36.             if (option == JFileChooser.APPROVE_OPTION) {  
  37.               if (chooser.getSelectedFile( )!=null)   
  38.                 statusbar.setText("You chose " + chooser.getSelectedFile( ).getName( ));  
  39.             }  
  40.             else {  
  41.               statusbar.setText("You canceled.");  
  42.             }  
  43.           }  
  44.         });  
  45.   
  46.         c.add(openButton);  
  47.         c.add(statusbar);  
  48.         setVisible(true);  
  49.       }  
  50.   
  51.       public static void main(String args[]) {  
  52.         MyFilterChooser mfc = new MyFilterChooser( );  
  53.       }  
  54.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值