请教一个关于建JTree类的问题..郁闷中..
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class CerateTree {
public CerateTree() {
super();
}
public JTree makeTree(){
DefaultMutableTreeNode root = new DefaultMutableTreeNode( "root ");
DefaultMutableTreeNode country = new DefaultMutableTreeNode( "国家 ");
root.add(country);
JTree tree = new JTree(root);
tree.setEditable(true);
return tree;
}
}
class test {
public static void main(String [] args){
JFrame frame = new JFrame();
frame.setSize(500,500);
JTree tree = new CerateDnDTree().makeTree();
JScrollPane sro = new JScrollPane(tree);
frame.add(sro);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
现在不想将构造树放在makeTree()方法中,而是构造成一个类.使tree直接能
tree = new CerateTree() 而不用 new CerateTree().makeTree()调用其方法.该怎么修改.. 改了好久老是有问题,谁能帮帮忙,郁闷中..
------解决方案--------------------想达到Tree tree = new CerateTree()的效果需要把CerateTree extends Tree 。
然后构造写成:
public CerateTree() {
super(this.getDefaultNode());
this.setEditable(true);
}
private DefaultMutableTreeNode getDefaultNode(){
DefaultMutableTreeNode root = new DefaultMutableTreeNode( "root ");
DefaultMutableTreeNode country = new DefaultMutableTreeNode( "国家 ");
root.add(country);
return root;
}
------解决方案--------------------继承JTree就好了