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(); 
} 
} 

本文按層次與模型建立的樹形選單,與建立選單欄類似,不知道小夥伴們有沒有掌握吶?

您可能感興趣的文章:

java實現遍歷樹形選單兩種實現程式碼分享java實現構造無限層級樹形選單Java構建樹形選單的例項程式碼(支援多級選單)Java遞迴如何正確輸出樹形選單java程式設計兩種樹形選單結構的轉換程式碼