FileChooser的简单使用

java 代码
  1. public class SimpleFileChooser extends JFrame {  
  2.   
  3.    public SimpleFileChooser( ) {  
  4.     super("File Chooser Test Frame");  
  5.     setSize(350200);  
  6.     setDefaultCloseOperation(EXIT_ON_CLOSE);  
  7.   
  8.     Container c = getContentPane( );  
  9.     c.setLayout(new FlowLayout( ));  
  10.       
  11.     JButton openButton = new JButton("Open");  
  12.     JButton saveButton = new JButton("Save");  
  13.     JButton dirButton = new JButton("Pick Dir");  
  14.     final JLabel statusbar =   
  15.                  new JLabel("Output of your selection will go here");  
  16.   
  17.     // Create a file chooser that opens up as an Open dialog.  
  18.     openButton.addActionListener(new ActionListener( ) {  
  19.       public void actionPerformed(ActionEvent ae) {  
  20.         JFileChooser chooser = new JFileChooser( );  
  21.         chooser.setMultiSelectionEnabled(true);  
  22.         int option = chooser.showOpenDialog(SimpleFileChooser.this);  
  23.         if (option == JFileChooser.APPROVE_OPTION) {  
  24.           File[] sf = chooser.getSelectedFiles( );  
  25.           String filelist = "nothing";  
  26.           if (sf.length > 0) filelist = sf[0].getName( );  
  27.           for (int i = 1; i < sf.length; i++) {  
  28.             filelist += ", " + sf[i].getName( );  
  29.           }  
  30.           statusbar.setText("You chose " + filelist);  
  31.         }  
  32.         else {  
  33.           statusbar.setText("You canceled.");  
  34.         }  
  35.       }  
  36.     });  
  37.   
  38.     // Create a file chooser that opens up as a Save dialog.  
  39.     saveButton.addActionListener(new ActionListener( ) {  
  40.       public void actionPerformed(ActionEvent ae) {  
  41.         JFileChooser chooser = new JFileChooser( );  
  42.         int option = chooser.showSaveDialog(SimpleFileChooser.this);  
  43.         if (option == JFileChooser.APPROVE_OPTION) {  
  44.           statusbar.setText("You saved " + ((chooser.getSelectedFile( )!=null)?  
  45.                             chooser.getSelectedFile( ).getName( ):"nothing"));  
  46.         }  
  47.         else {  
  48.           statusbar.setText("You canceled.");  
  49.         }  
  50.       }  
  51.     });  
  52.   
  53.     // Create a file chooser that allows you to pick a directory  
  54.     // rather than a file.  
  55.     dirButton.addActionListener(new ActionListener( ) {  
  56.       public void actionPerformed(ActionEvent ae) {  
  57.         JFileChooser chooser = new JFileChooser( );  
  58.         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);  
  59.         int option = chooser.showOpenDialog(SimpleFileChooser.this);  
  60.         if (option == JFileChooser.APPROVE_OPTION) {  
  61.           statusbar.setText("You opened " + ((chooser.getSelectedFile( )!=null)?  
  62.                             chooser.getSelectedFile( ).getName( ):"nothing"));  
  63.         }  
  64.         else {  
  65.           statusbar.setText("You canceled.");  
  66.         }  
  67.       }  
  68.     });  
  69.   
  70.     c.add(openButton);  
  71.     c.add(saveButton);  
  72.     c.add(dirButton);  
  73.     c.add(statusbar);  
  74.   }  
  75.   
  76.   public static void main(String args[]) {  
  77.     SimpleFileChooser sfc = new SimpleFileChooser( );  
  78.     sfc.setVisible(true);  
  79.   }  
  80. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值