1.目录选择框            
      

JFileChooser chooser=new JFileChooser(); 
               chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);              /**
             JFileChooser.DIRECTORIES_ONLY //只有目录
             JFileChooser.FILES_ONLY //只有文件
             JFileChooser.FILES_AND_DIRECTORIES //全部(默认值)
               */
               int result=chooser.showOpenDialog(null); 
               String fname=chooser.getName(chooser.getSelectedFile());
               System.out.println("fname--->"+fname);
               if(result==JFileChooser.APPROVE_OPTION)
               { 
                   
                    String filePath=chooser.getSelectedFile().getPath();
                    System.out.println("filePath--->"+filePath);
                    if(filePath.endsWith(".xml"))
                    {
                        System.out.println("filePath--->"+filePath);
                    }
               }
 Object[] options = { "OK", "CANCEL" };
               int results= JOptionPane.showOptionDialog(null, "是否保存?", "Warning",
               JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,null, options, options[0]);
               if(results==JOptionPane.OK_OPTION)
               {
                   doSave();
               }
               if(results==JOptionPane.NO_OPTION )
               {
                    doReturn();
               }


2.文件选择框
   

javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
         chooser.showOpenDialog(null);
           String fname=chooser.getName(chooser.getSelectedFile());