import java.awt.Dimension;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.BoxLayout;
import javax.swing.tree.TreePath;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
/*
JTree的构造函数:
JTree()
JTree(Hashtable value)
JTree(Object[] value)//只有这个构造函数可以创建多个根结点
JTree(TreeModel newModel)
JTree(TreeNode root)
JTree(TreeNode root, boolean asksAllowsChildren)
JTree(Vector value)
*/
public class JTreeDemo
{
public static void main (String[] args)
{
//构造函数:JTree()
JTree example1 = new JTree();
//构造函数:JTree(Object[] value)
Object[] letters= {"a", "b", "c", "d", "e"};
JTree example2 = new JTree (letters);
//构造函数:JTree(TreeNode root)(TreeNode空)
//用空结点创建树
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode();//定义树结点
JTree example3 = new JTree (node1);//用此树结点做参数调用 JTree的构造函数创建含有一个根结点的树
//构造函数:JTree(TreeNode root)(同上,只是TreeNode非空)
//用一个根结点创建树
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Color");
JTree example4 = new JTree (node2);//结点不可以颜色,默认为白面黑字
example4.setBackground (Color.lightGray);
//构造函数:JTree(TreeNode root, boolean asksAllowsChildren)(同上,只是TreeNode又有不同)
//使用DefaultMutableTreeNode类先用一个根结点创建树,设置为可添加孩子结点,再添加孩子结点
DefaultMutableTreeNode color = new DefaultMutableTreeNode("Color", true);
DefaultMutableTreeNode gray = new DefaultMutableTreeNode ("Gray");
color.add (gray);
color.add (new DefaultMutableTreeNode ("Red"));
gray.add (new DefaultMutableTreeNode ("Lightgray"));
gray.add (new DefaultMutableTreeNode ("Darkgray"));
color.add (new DefaultMutableTreeNode ("Green"));
JTree example5 = new JTree (color);
//构造函数:JTree(TreeNode root)(同上,只是TreeNode非空)
//通过逐个添加结点创建树
DefaultMutableTreeNode biology = new DefaultMutableTreeNode ("Biology");
DefaultMutableTreeNode animal = new DefaultMutableTreeNode ("Animal");
DefaultMutableTreeNode mammal = new DefaultMutableTreeNode ("Mammal");
DefaultMutableTreeNode horse = new DefaultMutableTreeNode ("Horse");
mammal.add (horse);
animal.add (mammal);
biology.add (animal);
JTree example6 = new JTree (biology);
horse.isLeaf();
horse.isRoot();
//构造函数:JTree(TreeModel newModel)
//用DefaultMutableTreeNodel类定义一个结点再用这个结点做参数定义一个用DefaultTreeMode
//创建一个树的模型,再用JTree的构造函数创建一个树
DefaultMutableTreeNode root = new DefaultMutableTreeNode ("Root1");
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode ("Child1");
DefaultMutableTreeNode child11 = new DefaultMutableTreeNode ("Child11");
DefaultMutableTreeNode child111 = new DefaultMutableTreeNode ("Child111");
root.add (child1); child1.add (child11); child11.add (child111);
DefaultTreeModel model = new DefaultTreeModel (root);
JTree example7 = new JTree (model);
JPanel panel = new JPanel();
panel.setLayout (new BoxLayout (panel, BoxLayout.X_AXIS));
panel.setPreferredSize (new Dimension (700, 400));
panel.add (new JScrollPane (example1));//JTree必须放在JScrollPane上
panel.add (new JScrollPane (example2));
panel.add (new JScrollPane (example3));
panel.add (new JScrollPane (example4));
panel.add (new JScrollPane (example5));
panel.add (new JScrollPane (example6));
panel.add (new JScrollPane (example7));
JFrame frame = new JFrame ("JTreeDemo");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setContentPane (panel);
frame.pack();
frame.show();
}
}
==========================这一种好简单,很好用======================================
import org.eclipse.swt.widgets.*;
public class Snippet33 {
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.open ();
DirectoryDialog dialog = new DirectoryDialog (shell);
dialog.setFilterPath ("c:\\"); //Windows specific
System.out.println ("RESULT=" + dialog.open ());
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
==================================================================================
楼上的用的是swt包,看见这个没org.eclipse.swt.widgets
下面是列表,你自己再改改吧,用的是jdk的jtree写的遍列所有盘文件夹
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
public class FileTree extends JDialog {
private JLabel path_ = new JLabel(" ");
private JTree tree_;
private static String filepath;
public FileTree() {
init();
}
private void init() {
JPanel labelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
labelPanel.setSize(300, 40);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JLabel current = new JLabel("Current Selection");
labelPanel.setLayout(new BorderLayout());
labelPanel.add(current, BorderLayout.NORTH);
labelPanel.add(path_, BorderLayout.SOUTH);
File[] roots = (new PFileSystemView()).getRoots();
FileNode nod = new FileNode (roots[0]);
nod.explore();
tree_ = new JTree(new DefaultTreeModel(nod));
tree_.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
JScrollPane sp = new JScrollPane(tree_);
sp.setBorder(BorderFactory.createEtchedBorder(Color.white, new Color(148, 145, 140)));
labelPanel.setBorder(BorderFactory.createEmptyBorder(0, 19, 0, 0));
JButton buttonOK = new JButton("OK");
buttonOK.setPreferredSize(new Dimension(70, 25));
JButton buttonCanel = new JButton("Canel");
buttonCanel.setPreferredSize(new Dimension(70, 25));
buttonPanel.add(buttonOK);
buttonPanel.add(buttonCanel);
//canel action
buttonCanel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//ok action
buttonOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.err.println(filepath);
}
});
tree_.setShowsRootHandles(true);
tree_.addTreeExpansionListener(new TreeExpansionListener() {
public void treeCollapsed(TreeExpansionEvent e) {
}
public void treeExpanded (TreeExpansionEvent e) {
TreePath path = e.getPath();
FileNode node = (FileNode)path.getLastPathComponent();
if (!node.isExplored()) {
DefaultTreeModel model = (
(DefaultTreeModel) tree_.getModel());
node.explore();
model.nodeStructureChanged(node);
}
}
});
tree_.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
JTree tree = (JTree)e.getSource();
int row = tree.getRowForLocation(e.getX(), e.getY());
if (row == -1) {
return;
}
TreePath path = tree.getPathForRow(row);
if (path == null) {
return;
}
FileNode node = (FileNode)path.getLastPathComponent();
if (node == null) {
return;
}
filepath = node.getString();
path_.setText(filepath);
}
});
getContentPane().add(sp, BorderLayout.CENTER);
getContentPane().add(labelPanel, BorderLayout.NORTH);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
setSize(350, 400);
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.setTitle("File Tree");
}
class FileNode extends DefaultMutableTreeNode {
private boolean explored_ = false;
public FileNode(File file) {
setUserObject (file);
}
public boolean getAllowChildren() {
return isDirectory();
}
public boolean isLeaf() {
return !isDirectory();
}
public File getFile() {
return (File)getUserObject();
}
public boolean isExplored() {
return explored_;
}
public boolean isDirectory() {
File file = getFile();
return file.isDirectory();
}
public String toString() {
File file = getFile ();
String filename = file.toString();
int index = filename.lastIndexOf("\\");
return (index != -1 && index != filename.length() - 1) ?
filename.substring(index + 1) :
filename;
}
public String getString() {
File file = getFile ();
String filename = file.getAbsolutePath();
return filename;
}
public void explore() {
if (!isDirectory()) {
return;
}
if (!isExplored()) {
File file = getFile ();
File [] children = file.listFiles();
for (int i = 0; i < children.length; ++i) {
if (children[i].isDirectory()) {
add(new FileNode (children[i]));
}
}
explored_ = true;
}
}
}
class PFileSystemView extends FileSystemView {
public File createNewFolder(File containingDir) throws IOException {
return null;
}
}
public static void main(String[] args) {
(new FileTree()).show(true);
}
}