JFileChooser fileChooser = new JFileChooser ();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setMultiSelectionEnabled(false);
fileChooser.showOpenDialog();

解决方案 »

  1.   

    我写了一段例子,愿对你有所帮助:import javax.swing.*;
    import java.awt.*;
    public class FileChooserTest extends JFrame {
      JPanel jPanel1 = new JPanel();
      BorderLayout borderLayout1 = new BorderLayout();
      BorderLayout borderLayout2 = new BorderLayout();
      JFileChooser jFileChooser1 = new JFileChooser();  public FileChooserTest() {
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      private void jbInit() throws Exception {
        this.getContentPane().setLayout(borderLayout1);
        jPanel1.setLayout(borderLayout2);
        this.getContentPane().add(jPanel1,  BorderLayout.CENTER);
        jPanel1.add(jFileChooser1, BorderLayout.CENTER);
      }
    }
      

  2.   

    上面掉了加一个main函数加以测试,同时详细的使用JFileChooser的方法可参看以下地址:
    http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html修改后程序如下:import javax.swing.*;
    import java.awt.*;
    public class FileChooserTest extends JFrame {
      JPanel jPanel1 = new JPanel();
      BorderLayout borderLayout1 = new BorderLayout();
      BorderLayout borderLayout2 = new BorderLayout();
      JFileChooser jFileChooser1 = new JFileChooser();  public FileChooserTest() {
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      private void jbInit() throws Exception {
        this.getContentPane().setLayout(borderLayout1);
        jPanel1.setLayout(borderLayout2);
        this.getContentPane().add(jPanel1,  BorderLayout.CENTER);
        jPanel1.add(jFileChooser1, BorderLayout.CENTER);
        this.setSize(400,300) ;
        this.setVisible(true);
      }  public static void main(String args[]){
          new FileChooserTest();
      }
    }
      

  3.   

    调用窗体吧,FileDialog还是什么,好像就能让你选择路径等等操作了.
    如果要自己写,用JFileChooser没错,用它得到目录结构,然后对他扩展,最好是一层一层扩展,以后点选那条就扩展那条(不要用递归的一次扩展完),用JTree显示出来,具体做法可以自己研究一下.
      

  4.   

    我昨天正好做了一个FileTreeNode类
    估计对你有用,就给你吧!
    //package myswing;
    import javax.swing.tree.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;public class FileTreeNode extends DefaultMutableTreeNode
    {

    private DefaultMutableTreeNode scanAndAddNode(File filename)
    { if(filename ==null)return null;
    DefaultMutableTreeNode parent = new DefaultMutableTreeNode(filename.getName());
    if(filename.isDirectory())
    {
    parent.setAllowsChildren(true);
    File[] subfiles = filename.listFiles();
    for(int i = 0;i<subfiles.length;i++)
    parent.add(scanAndAddNode(subfiles[i]));
    }
    return parent;
    }
    public FileTreeNode(File filename)
    {
    super(filename.getName());
    File[] subfiles = filename.listFiles();
    for(int i = 0;i<subfiles.length;i++)
    {
    add(scanAndAddNode(subfiles[i]));
    }
    }
    }
    测试时,加入下面这些代码
    public static void main(String[] args){
    JFrame f =new JFrame();
    File music = new File("j:\\music");
    JTree tree = new JTree(new FileTreeNode(music));
    JScrollPane sp = new JScrollPane(tree);
    f.getContentPane().add(sp);
    f.show();
    }
    缺点:程序启动时较慢,因为要实现遍历,可以考虑要程序运行是动态的添加文件节点,即你打开一个文件夹节点时,添加它下面的节点,一个文件夹下应该没有多少文件,这样做可以比较快,你自己改吧
      

  5.   

    那就看看JFileChooser的原码喽。
        /**
         * Constructs a <code>JFileChooser</code> using the given current directory
         * and <code>FileSystemView</code>.
         */
        public JFileChooser(File currentDirectory, FileSystemView fsv) {
    setup(fsv);
    setCurrentDirectory(currentDirectory);
        }
        /**
         * Performs common constructor initialization and setup.
         */
        protected void setup(FileSystemView view) {
    if(view == null) {
        view = FileSystemView.getFileSystemView();
            }
    setFileSystemView(view);
    updateUI(); 
    if(isAcceptAllFileFilterUsed()) {
        setFileFilter(getAcceptAllFileFilter());
    }
        }
        
    肯定可以构造出你想要的界面来