晚上回来接帖 :)

解决方案 »

  1.   

    gtlang78()在不?再给你看一个有挑战性的问题 哇哈哈
    http://community.csdn.net/Expert/topic/4193/4193834.xml?temp=.3597376
    JDK1.3 郁闷得不行的JDK 竟然连根目录都选不了:(
      

  2.   

    gtlang78()是个好同志!
    我超级崇拜他!
      

  3.   

    我自己写了一个目录选择对话框,你看看能不能用吧,针对jdk1.3作了修改,所以用1.4或者1.5看会有一些问题package dirchooser;
    import java.io.File;
    import java.util.*;import javax.swing.filechooser.FileSystemView;
    import javax.swing.tree.TreeNode;public class DirTreeNode implements TreeNode {
    private File dir = null; private List children = null; private DirTreeNode parent;

    public DirTreeNode(File dir, DirTreeNode parent) {
    this.dir = dir;
    this.parent = parent;
    } public TreeNode getChildAt(int childIndex) {
    initChildrenList(); return (TreeNode) children.get(childIndex);
    } public int getChildCount() {
    initChildrenList();

    return children.size();
    } public TreeNode getParent() {
    return parent;
    } public int getIndex(TreeNode node) {
    initChildrenList(); return children.indexOf(node);
    } public boolean getAllowsChildren() {
    return true;
    } public boolean isLeaf() {
    // if (dir != null && FileSystemView.getFileSystemView().isFloppyDrive(dir)) {
    // return false;
    // }
    if (dir != null && dir.getParent() == null && 
    (dir.getPath().toUpperCase().startsWith("A:") || 
     dir.getPath().toUpperCase().startsWith("B:"))) {
    return false;
    } initChildrenList(); return children.isEmpty();
    } public Enumeration children() {
    initChildrenList(); final Iterator iter = children.iterator();
    return new Enumeration() {
    public boolean hasMoreElements() {
    return iter.hasNext();
    } public Object nextElement() {
    return iter.next();
    }
    };
    } private void initChildrenList() {
    if (children == null) {
    File[] subDirs = null;
    if (dir == null) {
    subDirs = FileSystemView.getFileSystemView().getRoots();
    }
    else {
    subDirs = FileSystemView.getFileSystemView().getFiles(dir, false);
    }
    if (subDirs == null || subDirs.length == 0) {
    children = Collections.EMPTY_LIST;
    }
    else {
    children = new ArrayList();
    for (int i = 0; i < subDirs.length; i++) {
    if (subDirs[i]!= null && subDirs[i].isDirectory()) {
    children.add(new DirTreeNode(subDirs[i], this));
    }
    }
    }
    }
    } public String toString() {
    // return dir == null ? 
    // "" : FileSystemView.getFileSystemView().getSystemDisplayName(dir);
    if (dir == null) {
    return "我的电脑";
    }
    else if (dir.getParent() == null) {
    return dir.getPath();
    }
    else {
    return dir.getName();
    }
    } public boolean equals(Object obj) {
    if (obj instanceof DirTreeNode) {
    DirTreeNode t = (DirTreeNode) obj;
    return dir == null ? t.dir == null : dir.equals(t.dir);
    }
    return false;
    } public int hashCode() {
    return dir == null ? 0 : dir.hashCode();
    } public File getDir()
    {
    return dir;
    }
    }
      

  4.   

    package dirchooser;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;import javax.swing.*;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.DefaultTreeCellRenderer;
    import javax.swing.tree.TreePath;public class DirChooserDialog
    extends JDialog
    {
    private JLabel descrLabel = new JLabel("", JLabel.LEFT);
    private JTree dirTree = null;
    private JScrollPane scrollPane = null;

    private JButton okBtn = new JButton("确定");
    private JButton cancleBtn = new JButton("取消");

    private JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    private boolean okBtnPressed;

    public DirChooserDialog(Frame owner, String dialogTitle, String descrStr) {
    super(owner, dialogTitle, true); initComponents(descrStr);
    } public DirChooserDialog(Dialog owner, String dialogTitle, String descrStr) {
    super(owner, dialogTitle, true);

    initComponents(descrStr);
    } private void initComponents(String descrStr)
    {
    descrLabel.setText(descrStr); btnPanel.add(okBtn);
    btnPanel.add(cancleBtn);

    dirTree = new JTree(new DirTreeNode(null, null));
    dirTree.setCellRenderer(new DefaultTreeCellRenderer() {
    public Component getTreeCellRendererComponent(
    JTree tree, Object value, boolean sel, boolean expanded, 
    boolean leaf, int row, boolean hasFocus) { super.getTreeCellRendererComponent(
    tree, value, sel, expanded, leaf, row, hasFocus);

    DirTreeNode node = (DirTreeNode) value;
    if (node.getDir() != null) {
    // setIcon(FileSystemView.getFileSystemView().getSystemIcon(node.dir));
    if (node.getDir().getParent() == null) {
    setIcon(UIManager.getIcon("FileView.hardDriveIcon"));
    }
    else {
    setIcon(UIManager.getIcon("FileView.directoryIcon"));
    }
    }
    else {
    setIcon(UIManager.getIcon("FileView.computerIcon"));
    }
    return this;
    }
    });
    dirTree.addTreeSelectionListener(new TreeSelectionListener() {
    public void valueChanged(TreeSelectionEvent e)
    {
    updateOKBtnState();
    }
    });
    // dirTree.setRootVisible(false);
    JScrollPane scrollPane = new JScrollPane(dirTree);
    scrollPane.setPreferredSize(new Dimension(300, 300));

    okBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    okBtnPressed = true;
    dispose();
    }
    });

    cancleBtn.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    okBtnPressed = false;
    dispose();
    }
    });

    ((JComponent) getContentPane()).setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    getContentPane().setLayout(new BorderLayout(5, 5)); getContentPane().add(descrLabel, BorderLayout.NORTH);
    getContentPane().add(scrollPane, BorderLayout.CENTER);
    getContentPane().add(btnPanel, BorderLayout.SOUTH);

    setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

    updateOKBtnState();
    pack();
    } private void updateOKBtnState()
    {
    okBtn.setEnabled(getSelectedDir() != null);
    } public File showDirChooserDialog() {
    okBtnPressed = false;

    Rectangle bounds = getParent().getBounds();
    setLocation(bounds.x + (bounds.width - getWidth()) / 2 ,
            bounds.y + (bounds.height - getHeight()) / 2);
    setVisible(true); if (okBtnPressed) {
    return getSelectedDir();
    }
    else {
    return null;
    }
    }

    public File getSelectedDir() {
    TreePath path = dirTree.getSelectionPath();
    if (path == null || path.getPathCount() == 1) {
    return null;
    }
    DirTreeNode node = (DirTreeNode) path.getLastPathComponent(); return node.getDir();
    }

    public void setSelectedDir(File dir) {
    List pathList = new LinkedList();
    do {
    pathList.add(0, dir);
    dir = dir.getParentFile();
    } while (dir != null);

    TreePath path = new TreePath(dirTree.getModel().getRoot());
    for (Iterator iter = pathList.iterator(); iter.hasNext();) {
    File d = (File) iter.next();
    path = path.pathByAddingChild(
    new DirTreeNode(d, (DirTreeNode) path.getLastPathComponent()));
    }

    dirTree.setSelectionPath(path);
    dirTree.scrollPathToVisible(path);
    }

    public static void main(String[] args)
    {
    try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
    e.printStackTrace();
    } final JFrame f = new JFrame("DirChooserTest"); JButton btn = new JButton("ChooseDir");
    btn.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    DirChooserDialog dlg = new DirChooserDialog(f, "DirChooserDialog", "选择保存文件的目录:");
    //dlg.setSelectedDir(new File("F:/temp"));
    System.out.println(dlg.showDirChooserDialog());
    }
    }); f.getContentPane().add(btn, BorderLayout.CENTER);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setLocation(500, 500);
    f.show();
    }
    }
      

  5.   

    天啊 gtlang78()老大 我对您的景仰真是有如滔滔江水连绵不绝 又有如洪水泛滥一发不可收拾啦
    真是谢谢你了
    下午我解决了个比较郁闷的问题单(哈哈 小有点成就感) 所以一直没空上网看 
    结果上来一看 真的是感动啊 感动感动。感动感动。感动感动。感动感动。感动感动。
      

  6.   

    高手啊高手 哎不知道偶猴年马月能够练到这般身手
    要加油加油了 GOOD GOOD STUDY ,DAY DAY UP ~