利用Swing包中的JTree组件编写一个树形目录。
4个回答
展开全部
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class TreeModel {
public static DefaultTreeModel treeModel(){
DefaultMutableTreeNode china = new DefaultMutableTreeNode("中国");
DefaultMutableTreeNode chongqing = new DefaultMutableTreeNode("重庆");
DefaultMutableTreeNode beijing = new DefaultMutableTreeNode("北京");
DefaultMutableTreeNode tianjing = new DefaultMutableTreeNode("天津");
DefaultMutableTreeNode shanxi = new DefaultMutableTreeNode("陕西");
DefaultMutableTreeNode hunan = new DefaultMutableTreeNode("湖南");
DefaultMutableTreeNode guangdong = new DefaultMutableTreeNode("广东");
DefaultMutableTreeNode xian = new DefaultMutableTreeNode("西安");
DefaultMutableTreeNode ankang = new DefaultMutableTreeNode("安康");
DefaultMutableTreeNode guanzhou = new DefaultMutableTreeNode("广州");
DefaultMutableTreeNode zhongshan = new DefaultMutableTreeNode("中山");
DefaultMutableTreeNode shenzhen = new DefaultMutableTreeNode("深圳");
DefaultMutableTreeNode fushan = new DefaultMutableTreeNode("佛山");
china.add(beijing);
china.add(chongqing);
china.add(tianjing);
china.add(shanxi);
china.add(hunan);
china.add(guangdong);
shanxi.add(xian);
shanxi.add(ankang);
guangdong.add(guanzhou);
guangdong.add(zhongshan);
guangdong.add(shenzhen);
guangdong.add(fushan);
//选择根结点,再把它的子结点添加到它的目录下
DefaultTreeModel tree = new DefaultTreeModel(china);
return tree;
}
==========================================
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTree;
public class father {
static JFrame aWindow = new JFrame("JTextField Test");
static JTree tree = new JTree(TreeModel.treeModel());
public static void main(String[] args) {
aWindow.getContentPane().add(tree);
aWindow.setSize(1000,500);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flow = new FlowLayout();
aWindow.getContentPane().setLayout(flow);
aWindow.setVisible(true);
}
}
//我也在学java,现在刚好学到这里,
import javax.swing.tree.DefaultTreeModel;
public class TreeModel {
public static DefaultTreeModel treeModel(){
DefaultMutableTreeNode china = new DefaultMutableTreeNode("中国");
DefaultMutableTreeNode chongqing = new DefaultMutableTreeNode("重庆");
DefaultMutableTreeNode beijing = new DefaultMutableTreeNode("北京");
DefaultMutableTreeNode tianjing = new DefaultMutableTreeNode("天津");
DefaultMutableTreeNode shanxi = new DefaultMutableTreeNode("陕西");
DefaultMutableTreeNode hunan = new DefaultMutableTreeNode("湖南");
DefaultMutableTreeNode guangdong = new DefaultMutableTreeNode("广东");
DefaultMutableTreeNode xian = new DefaultMutableTreeNode("西安");
DefaultMutableTreeNode ankang = new DefaultMutableTreeNode("安康");
DefaultMutableTreeNode guanzhou = new DefaultMutableTreeNode("广州");
DefaultMutableTreeNode zhongshan = new DefaultMutableTreeNode("中山");
DefaultMutableTreeNode shenzhen = new DefaultMutableTreeNode("深圳");
DefaultMutableTreeNode fushan = new DefaultMutableTreeNode("佛山");
china.add(beijing);
china.add(chongqing);
china.add(tianjing);
china.add(shanxi);
china.add(hunan);
china.add(guangdong);
shanxi.add(xian);
shanxi.add(ankang);
guangdong.add(guanzhou);
guangdong.add(zhongshan);
guangdong.add(shenzhen);
guangdong.add(fushan);
//选择根结点,再把它的子结点添加到它的目录下
DefaultTreeModel tree = new DefaultTreeModel(china);
return tree;
}
==========================================
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTree;
public class father {
static JFrame aWindow = new JFrame("JTextField Test");
static JTree tree = new JTree(TreeModel.treeModel());
public static void main(String[] args) {
aWindow.getContentPane().add(tree);
aWindow.setSize(1000,500);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flow = new FlowLayout();
aWindow.getContentPane().setLayout(flow);
aWindow.setVisible(true);
}
}
//我也在学java,现在刚好学到这里,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你是想要源代码是吧?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
编写类继承JTREE
重写JTREEUI 类
自己编写布局管理器
重写JTREEUI 类
自己编写布局管理器
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
编写类继承JTREE
重写JTREEUI 类
自己编写布局管理器
重写JTREEUI 类
自己编写布局管理器
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询