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()调用其方法.该怎么修改.. 改了好久老是有问题,谁能帮帮忙,郁闷中..
----------------解决方案--------------------------------------------------------
HELP...
----------------解决方案--------------------------------------------------------
你把那些构造的内容直接放在super的下面就是了阿
----------------解决方案--------------------------------------------------------
使用继承不就可以了吗?
----------------解决方案--------------------------------------------------------
我也是想用继承的 但是不知道为什么出来的JTree不是我建的那个树 很郁闷
我都不知道那个树是从什么地方出来的 汗死..
----------------解决方案--------------------------------------------------------
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
class CerateTree extends JTree{
static DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
static DefaultMutableTreeNode country = new DefaultMutableTreeNode("国家");
static{
root.add(country);
}
public CerateTree() {
super(root);
setEditable(true);
}
}
public class test {
public static void main(String [] args){
JFrame frame = new JFrame();
frame.setSize(500,500);
JTree tree = new CerateTree();
JScrollPane sro = new JScrollPane(tree);
frame.add(sro);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
----------------解决方案--------------------------------------------------------