这是我的代码我无法在jtree中显示当前系统中的所有驱动器,它返回的是一些随机的java文件,我是一个初学者,并且不知道它是如何完成的,等待帮助..
import java.awt.GridLayout;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
public class JTreeLits extends JPanel
{
JTree tree;
public JTreeLits()
{
super(new GridLayout(1,0));
DefaultMutableTreeNode rooter = new DefaultMutableTreeNode("FileSystem");
DefaultMutableTreeNode parent;
File [] roots = File.listRoots();
parent = new DefaultMutableTreeNode(roots);
rooter.add(parent);
tree = new JTree(rooter);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
add(tree);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("TreeDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JTreeLits());
frame.pack();
frame.setVisible(true);
frame.setSize(250,400);
}
public static void main(String[] args)
{
createAndShowGUI();
}
}
答案 0 :(得分:2)
查看FileSystemView.getRoots()
1 ,其中:
返回此系统上的所有根分区。例如,在Windows上,这将是“桌面”文件夹,而在DOS上,这将是A:到Z:驱动器。