Java树形菜单的创建
程序员文章站
2024-03-06 11:10:07
功能:实现创建一个树形菜单
说明:创建树形菜单结构与创建菜单栏类似,是按层次与模型创建的。
通过defaultmutabletreenode类创建根节点、子节点和孙...
功能:实现创建一个树形菜单
说明:创建树形菜单结构与创建菜单栏类似,是按层次与模型创建的。
通过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(); } }
本文按层次与模型创建的树形菜单,与创建菜单栏类似,不知道小伙伴们有没有掌握呐?