功能:实现创建一个树形菜单
说明:创建树形菜单结构与创建菜单栏类似,是按层次与模型创建的。
通过DefaultMutableTreeNode类创建根节点、子节点和孙节点对象,再通过DefaultTreeModel
类利用根节点创建树模型对象,然后通过treeModel.insertNodeInto方法将节点对象插入树模型中。
效果图:
代码:
import java.awt.*; import javax.swing.*; import javax.swing.tree.*; import javax.swing.event.*; public class Tree extends JFrame implements TreeSelectionListener { private JLabel label; public Tree() { super("树形菜单"); setSize(400,400); Container container = getContentPane(); //创建根节点和子节点 DefaultMutableTreeNode root = new DefaultMutableTreeNode("文本编辑器"); DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("文件"); DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("编辑"); //利用根节点创建TreeModel DefaultTreeModel treeModel = new DefaultTreeModel(root); //插入子节点node1,node2 treeModel.insertNodeInto(node1,root,root.getChildCount()); treeModel.insertNodeInto(node2,root,root.getChildCount()); //创建节点node1的子节点并插入 DefaultMutableTreeNode leafnode = new DefaultMutableTreeNode("打开"); treeModel.insertNodeInto(leafnode,node1,node1.getChildCount()); leafnode = new DefaultMutableTreeNode("保存"); treeModel.insertNodeInto(leafnode,node1,node1.getChildCount()); leafnode = new DefaultMutableTreeNode("另存为"); treeModel.insertNodeInto(leafnode,node1,node1.getChildCount()); leafnode = new DefaultMutableTreeNode("关闭"); treeModel.insertNodeInto(leafnode,node1,node1.getChildCount()); //创建节点node2的子节点并插入 leafnode = new DefaultMutableTreeNode("剪切"); treeModel.insertNodeInto(leafnode,node2,node2.getChildCount()); leafnode = new DefaultMutableTreeNode("复制"); treeModel.insertNodeInto(leafnode,node2,node2.getChildCount()); leafnode = new DefaultMutableTreeNode("粘贴"); treeModel.insertNodeInto(leafnode,node2,node2.getChildCount()); //创建树对象 JTree tree = new JTree(treeModel); //设置Tree的选择为一次只能选择一个节点 tree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION); //注册监听器 tree.addTreeSelectionListener(this); tree.setRowHeight(20); //创建节点绘制对象 DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer)tree.getCellRenderer(); //设置字体 cellRenderer.setFont(new Font("Serif",Font.PLAIN,14)); cellRenderer.setBackgroundNonSelectionColor(Color.white); cellRenderer.setBackgroundSelectionColor(Color.yellow); cellRenderer.setBorderSelectionColor(Color.red); //设置选或不选时,文字的变化颜色 cellRenderer.setTextNonSelectionColor(Color.black); cellRenderer.setTextSelectionColor(Color.blue); //把树对象添加到内容面板 container.add(new JScrollPane(tree)); //创建标签 label = new JLabel("你当前选择的节点为:",JLabel.CENTER); label.setFont(new Font("Serif",Font.PLAIN,14)); container.add(label,BorderLayout.SOUTH); setVisible(true); //设置可见 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置窗口关闭动作 } //处理TreeSelectionEvent事件 public void valueChanged(TreeSelectionEvent event) { JTree tree = (JTree)event.getSource(); //获取目前选取的节点 DefaultMutableTreeNode selectionNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); String nodeName = selectionNode.toString(); label.setText("你当前选取的节点为: "+nodeName); } public static void main(String args[]) { Tree d = new Tree(); } }
本文按层次与模型创建的树形菜单,与创建菜单栏类似,不知道小伙伴们有没有掌握呐?
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索java
, java树形菜单
, java树形菜单遍历
, web树形菜单
java动态树形菜单
js xml创建树形菜单、java树形菜单、java 树形菜单 递归、java实现树形菜单、java swing 树形菜单,以便于您获取更多的相关知识。
时间: 2024-09-27 15:24:16