程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java樹形菜單的創立

Java樹形菜單的創立

編輯:關於JAVA

Java樹形菜單的創立。本站提示廣大學習愛好者:(Java樹形菜單的創立)文章只能為提供參考,不一定能成為您想要的結果。以下是Java樹形菜單的創立正文


功效:完成創立一個樹形菜單
解釋:創立樹形菜單構造與創立菜單欄相似,是按條理與模子創立的。
經由過程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(); 
 } 
 
} 

本文按條理與模子創立的樹形菜單,與創立菜單欄相似,不曉得小同伴們有無控制吶?

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved