欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java樹形菜單的創(chuàng)建

 更新時(shí)間:2021年05月19日 11:55:52   作者:IT_xiao小巫  
這篇文章主要為大家詳細(xì)介紹了Java圖形用戶界面中樹形菜單的創(chuàng)建樹形菜單的創(chuàng)建,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java樹形菜單的創(chuàng)建代碼,供大家參考,具體內(nèi)容如下

功能:實(shí)現(xiàn)創(chuàng)建一個(gè)樹形菜單
說明:創(chuàng)建樹形菜單結(jié)構(gòu)與創(chuàng)建菜單欄類似,是按層次與模型創(chuàng)建的。
通過DefaultMutableTreeNode類創(chuàng)建根節(jié)點(diǎn)、子節(jié)點(diǎn)和孫節(jié)點(diǎn)對(duì)象,再通過DefaultTreeModel
類利用根節(jié)點(diǎn)創(chuàng)建樹模型對(duì)象,然后通過treeModel.insertNodeInto方法將節(jié)點(diǎn)對(duì)象插入樹模型中。

效果圖:

代碼:

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(); 
 
  //創(chuàng)建根節(jié)點(diǎn)和子節(jié)點(diǎn) 
  DefaultMutableTreeNode root = new DefaultMutableTreeNode("文本編輯器"); 
  DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("文件"); 
  DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("編輯"); 
  //利用根節(jié)點(diǎn)創(chuàng)建TreeModel 
  DefaultTreeModel treeModel = new DefaultTreeModel(root); 
  //插入子節(jié)點(diǎn)node1,node2 
  treeModel.insertNodeInto(node1,root,root.getChildCount()); 
  treeModel.insertNodeInto(node2,root,root.getChildCount()); 
 
  //創(chuàng)建節(jié)點(diǎn)node1的子節(jié)點(diǎn)并插入 
  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("關(guān)閉"); 
  treeModel.insertNodeInto(leafnode,node1,node1.getChildCount()); 
 
  //創(chuàng)建節(jié)點(diǎn)node2的子節(jié)點(diǎn)并插入 
  leafnode = new DefaultMutableTreeNode("剪切"); 
  treeModel.insertNodeInto(leafnode,node2,node2.getChildCount()); 
  leafnode = new DefaultMutableTreeNode("復(fù)制"); 
  treeModel.insertNodeInto(leafnode,node2,node2.getChildCount()); 
  leafnode = new DefaultMutableTreeNode("粘貼"); 
  treeModel.insertNodeInto(leafnode,node2,node2.getChildCount()); 
 
  //創(chuàng)建樹對(duì)象 
  JTree tree = new JTree(treeModel); 
  //設(shè)置Tree的選擇為一次只能選擇一個(gè)節(jié)點(diǎn) 
  tree.getSelectionModel().setSelectionMode( 
       TreeSelectionModel.SINGLE_TREE_SELECTION); 
  //注冊(cè)監(jiān)聽器 
  tree.addTreeSelectionListener(this); 
 
  tree.setRowHeight(20); 
 
  //創(chuàng)建節(jié)點(diǎn)繪制對(duì)象 
  DefaultTreeCellRenderer cellRenderer = 
       (DefaultTreeCellRenderer)tree.getCellRenderer(); 
 
  //設(shè)置字體 
  cellRenderer.setFont(new Font("Serif",Font.PLAIN,14)); 
  cellRenderer.setBackgroundNonSelectionColor(Color.white); 
  cellRenderer.setBackgroundSelectionColor(Color.yellow); 
  cellRenderer.setBorderSelectionColor(Color.red); 
 
  //設(shè)置選或不選時(shí),文字的變化顏色 
  cellRenderer.setTextNonSelectionColor(Color.black); 
  cellRenderer.setTextSelectionColor(Color.blue); 
   
  //把樹對(duì)象添加到內(nèi)容面板 
  container.add(new JScrollPane(tree)); 
 
  //創(chuàng)建標(biāo)簽 
  label = new JLabel("你當(dāng)前選擇的節(jié)點(diǎn)為:",JLabel.CENTER); 
  label.setFont(new Font("Serif",Font.PLAIN,14)); 
  container.add(label,BorderLayout.SOUTH); 
 
  setVisible(true); //設(shè)置可見 
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設(shè)置窗口關(guān)閉動(dòng)作 
 } 
 
 //處理TreeSelectionEvent事件 
 public void valueChanged(TreeSelectionEvent event) 
 { 
  JTree tree = (JTree)event.getSource(); 
  //獲取目前選取的節(jié)點(diǎn) 
  DefaultMutableTreeNode selectionNode = 
   (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); 
  String nodeName = selectionNode.toString(); 
  label.setText("你當(dāng)前選取的節(jié)點(diǎn)為: "+nodeName); 
 } 
 
 public static void main(String args[]) 
 { 
  Tree d = new Tree(); 
 } 
 
} 

本文按層次與模型創(chuàng)建的樹形菜單,與創(chuàng)建菜單欄類似,不知道小伙伴們有沒有掌握吶?

相關(guān)文章

  • Java?spring?boot實(shí)現(xiàn)批量刪除功能詳細(xì)示例

    Java?spring?boot實(shí)現(xiàn)批量刪除功能詳細(xì)示例

    這篇文章主要給大家介紹了關(guān)于Java?spring?boot實(shí)現(xiàn)批量刪除功能的相關(guān)資料,文中通過代碼以及圖文將實(shí)現(xiàn)的方法介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2023-08-08
  • RabbitMQ使用SpringAMQP的配置方法

    RabbitMQ使用SpringAMQP的配置方法

    這篇文章主要介紹了RabbitMQ使用SpringAMQP的配置方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-03-03
  • Java實(shí)現(xiàn)布隆過濾器的示例詳解

    Java實(shí)現(xiàn)布隆過濾器的示例詳解

    布隆過濾器(Bloom?Filter)是1970年由布隆提出來的,實(shí)際上是由一個(gè)很長(zhǎng)的二進(jìn)制數(shù)組+一系列hash算法映射函數(shù),用于判斷一個(gè)元素是否存在于集合中。本文主要介紹了Java實(shí)現(xiàn)布隆過濾器的示例代碼,希望對(duì)大家有所幫助
    2023-03-03
  • IDEA使用學(xué)生郵箱無法注冊(cè)問題:JetBrains Account connection error: 拒絕連接

    IDEA使用學(xué)生郵箱無法注冊(cè)問題:JetBrains Account connection error: 拒絕連接

    這篇文章主要介紹了IDEA使用學(xué)生郵箱無法注冊(cè)問題:JetBrains Account connection error: 拒絕連接,文中通過圖文及示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Mybatis框架及原理實(shí)例分析

    Mybatis框架及原理實(shí)例分析

    這篇文章主要介紹了Mybatis框架及原理實(shí)例分析,需要的朋友可以參考下
    2017-08-08
  • mybatis Reflector反射類的具體使用

    mybatis Reflector反射類的具體使用

    Reflector類是MyBatis反射模塊的核心,負(fù)責(zé)處理類的元數(shù)據(jù),以實(shí)現(xiàn)屬性與數(shù)據(jù)庫(kù)字段之間靈活映射的功能,本文主要介紹了mybatis Reflector反射類的具體使用,感興趣的可以了解一下
    2024-02-02
  • 如何使用ActiveMQ中間件方式發(fā)送郵件

    如何使用ActiveMQ中間件方式發(fā)送郵件

    這篇文章主要介紹了如何使用ActiveMQ中間件方式發(fā)送郵件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • java計(jì)算兩個(gè)時(shí)間相差天數(shù)的方法匯總

    java計(jì)算兩個(gè)時(shí)間相差天數(shù)的方法匯總

    這篇文章主要介紹了java計(jì)算兩個(gè)時(shí)間相差天數(shù)的方法,感興趣的小伙伴們可以參考一下
    2015-11-11
  • 在java中實(shí)現(xiàn)C#語法里的按引用傳遞參數(shù)的方法

    在java中實(shí)現(xiàn)C#語法里的按引用傳遞參數(shù)的方法

    下面小編就為大家?guī)硪黄趈ava中實(shí)現(xiàn)C#語法里的按引用傳遞參數(shù)的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-09-09
  • java實(shí)現(xiàn)table添加右鍵點(diǎn)擊事件監(jiān)聽操作示例

    java實(shí)現(xiàn)table添加右鍵點(diǎn)擊事件監(jiān)聽操作示例

    這篇文章主要介紹了java實(shí)現(xiàn)table添加右鍵點(diǎn)擊事件監(jiān)聽操作,結(jié)合實(shí)例形式分析了Java添加及使用事件監(jiān)聽相關(guān)操作技巧,需要的朋友可以參考下
    2018-07-07

最新評(píng)論