编了一个实现JTree的类,但树型结构总是不出现,请教原因,代码如下:
import javax.swing.*;
import javax.swing.tree.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
//construc function
public class DirTree extends JFrame implements ActionListener{
public DirTree(){
super("Directory Assistance");
Container c=getContentPane();
root=new DefaultMutableTreeNode("J2SDK");
treeModel=new DefaultTreeModel(root);
tree=new JTree(treeModel);
MouseListener m1=new MouseAdapter(){
public void mouseClicked(MouseEvent e){
showDetails(tree.getPathForLocation(e.getX(),e.getY()));
}
};
tree.addMouseListener(m1);
buildTree();
controls=new JPanel();
box=new JCheckBox(showString);
init();
c.add(controls,BorderLayout.NORTH);
c.add(tree,BorderLayout.CENTER);
show();
}
//add initialization method
private void init(){
tree.setFont(new Font("Dialog",Font.BOLD,12));
controls.add(box);
addButton(closeString);
controls.setBackground(Color.lightGray);
controls.setLayout(new FlowLayout());
setSize(400,400);
}
//Button action listen method
public void actionPerformed(ActionEvent e){
String cmd=e.getActionCommand();
if(cmd.equals(closeString))
dispose();
}
/*add button method*/
private void addButton(String n){
JButton b=new JButton(n);
b.setFont(new Font("Dialog",Font.BOLD,12));
b.addActionListener(this);
controls.add(b);
}
//at root build tree
private void buildTree(){
File list[]=new File("f:\\j2sdk").listFiles();
for(int i=0;i<list.length;i++){
if(list[i].exists())
buildTree(list[i],root,i);
}
}
//build the other node
private void buildTree(File f,DefaultMutableTreeNode parent,int where){
DefaultMutableTreeNode n=new DefaultMutableTreeNode(f.toString());
treeModel.insertNodeInto(n,parent,where);
if(f.isDirectory()){
String list[]=f.list();
for(int i=0;i<list.length;i++)
buildTree(new File(f,list[i]),n,i);
}
}
//show a file's detail
private void showDetails(TreePath p){
if(p==null)
return;
File f=new File(p.getLastPathComponent().toString());
JOptionPane.showMessageDialog(this,f.getPath()+"\n "+getAttributes(f));
}
//get a file's content to string
private String getAttributes(File f){
String t=" ";
if(f.isDirectory())
t+="Directorty";
else
t+="Nondirectory file";
t+="\n";
if(!f.canRead())
t+="not ";
t+="Readable\n ";
if(!f.canWrite())
t+="not ";
t+="Writeable\n ";
if(!f.isDirectory())
t+="Size in Bytes: "+f.length()+"\n ";
else{
t+="Contains files number:\n ";
String[] content=f.list();
t+=content.length;
}
return t;
}
//method main
public static void main(String a[]){
new DirTree();
}
//declare the field
private JCheckBox box;
private JTree tree;
private DefaultMutableTreeNode root;
private DefaultTreeModel treeModel;
private JPanel controls;
private String file;
private static final String closeString="Close";
private static final String showString="Show Details";
}
參考答案:调试了一下你的程序,问题在于你的MouseListener使得JTree的default MouseListener 的双击事件无法被触发, 改成右键触发showDetail就没有问题了,如下
MouseListener m1 = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton()==MouseEvent.BUTTON3)
showDetails(tree.getPathForLocation(e.getX(), e.getY()));
}
};