导航栏

import javax.swing.JSplitPane;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.*;

private JSplitPane getJSplitPane()
{
if (jSplitPane == null)
{
   jSplitPane = new JSplitPane();
   jSplitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
   jSplitPane.setLeftComponent(getJScrollTreePane());
   jSplitPane.setRightComponent(getJSplitSubPane());
   jSplitPane.setDividerSize(2); // 句柄宽度
   jSplitPane.setDividerLocation(220); //左侧控件宽度
}
return jSplitPane;
}

private JScrollPane getJScrollTreePane()
{
if (jScrollTreePane == null)
{
   DefaultMutableTreeNode root = new DefaultMutableTreeNode("药品进销存管理系统");// 生成根节点
   DefaultMutableTreeNode childNode = new DefaultMutableTreeNode("基本信息管理");
   String[] nodes = {"供应商基本信息", "药品类别管理", "药品字典管理", "人员管理"};
   for(String node : nodes)
   {
    childNode.add(new DefaultMutableTreeNode(node));
   }
   root.add(childNode);
   childNode = new DefaultMutableTreeNode("药品采购管理");
   nodes = new String[] {"采购订单管理", "采购订单查询", "采购订单统计"};
   for(String node : nodes)
   {
    childNode.add(new DefaultMutableTreeNode(node));
   }
   root.add(childNode);
   childNode = new DefaultMutableTreeNode("库存管理");
   nodes = new String[] {"入库管理", "出库管理", "库存盘点"};
   for(String node : nodes)
   {
    childNode.add(new DefaultMutableTreeNode(node));
   }
   root.add(childNode);
   childNode = new DefaultMutableTreeNode("系统维护");
   nodes = new String[] {"系统日志", "注销...", "系统退出"};
   for(String node : nodes)
   {
    childNode.add(new DefaultMutableTreeNode(node));
   }
   root.add(childNode);
   jTree = new JTree(root);
   DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer)jTree.getCellRenderer();// 得到树的绘制器
   ImageIcon icon = new ImageIcon("./image/hide.gif");
   renderer.setOpenIcon(icon);      // 设置打开节点的图标
   renderer.setClosedIcon(icon);   // 设置关闭节点的图标
   icon = new ImageIcon("./image/show.gif");
   renderer.setLeafIcon(icon);   // 设置叶子节点的图标
   renderer.setTextSelectionColor(Color.BLUE);
   renderer.setFont(new Font("Dialog", Font.TRUETYPE_FONT, 14));
   jTree.collapseRow(0);     // 先关闭根节点
   for(int row =0; row < jTree.getRowCount();++row)
   {
    jTree.expandRow(row); // 展开全部子节点
   }
   jTree.addTreeSelectionListener(new TreeSelectionListener()
   {
    public void valueChanged(TreeSelectionEvent e)
    {
     DefaultMutableTreeNode node = (DefaultMutableTreeNode)jTree.getLastSelectedPathComponent();
     if(node.isLeaf()) // 判断选中的节点是否为叶子节点。(node.getDepth() == 0)
      selectionEvent(node.toString());
    }
   });
   jScrollTreePane = new JScrollPane();
   jScrollTreePane.setViewportView(jTree);
}
return jScrollTreePane;
}

private void selectionEvent(String name)
{
if(name.equals("系统退出"))
{
   if(JOptionPane.showConfirmDialog(this, "确实要退出药品进销存管理系统吗?", "系统提示", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == 0)
    System.exit(0);
}
}

你可能感兴趣的:(导航)