2006年11月27日

          讀取XML來創建菜單

          Type:
          完整代碼
          Category:
          其它
          License:
          GNU General Public License
          Language:
          Java
          ?
          Description:
          通過讀取XML文檔來創建菜單
          XML的DTD規范是:
          <!ELEMENT root (top+)>
          <!ELEMENT top ((menu*,separator*,popup*)*)>
          <!ATTLIST top name ID #REQUIRED>
          <!ELEMENT menu (#PCDATA)>
          <!ELEMENT separator EMPTY>
          <!ELEMENT popup ((menu+,separator*)*)>
          <!ATTLIST popup name ID #REQUIRED>

          ?

          最新代碼版本: :1.0

          				
          						/**
           * 標題:制作菜單
           * 
           * 注:通過XML就可以制作出菜單來
           * 
           * 作者:元杰(夏祥均)
           * 時間:2006.09.28
           */
          package yuanjie.myconfig.frame;
          
          import java.awt.event.ActionListener;
          
          import javax.swing.JMenu;
          import javax.swing.JMenuBar;
          import javax.swing.JMenuItem;
          import javax.xml.parsers.DocumentBuilder;
          import javax.xml.parsers.DocumentBuilderFactory;
          import javax.xml.parsers.ParserConfigurationException;
          
          import org.w3c.dom.DOMException;
          import org.w3c.dom.Document;
          import org.w3c.dom.Element;
          import org.w3c.dom.NodeList;
          
          public class CreateMenu 
          {
          	private String fileName = null;
          	private ActionListener actionListener = null;
          	private JMenuBar rootMenu = null;
          	//構造函數
          	public CreateMenu(String fileName, ActionListener action)
          	{
          		this.fileName = fileName;
          		this.actionListener = action;
          		this.rootMenu = new JMenuBar();
          		init();
          	}
          	//返回菜單的內容
          	public JMenuBar getMenuBar() 
          	{
          		return rootMenu;
          	}
          	//初始化菜單
          	private void init()
          	{
          		//讀取XML文檔
          		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          		DocumentBuilder db = null;
          		try{
          			db = dbf.newDocumentBuilder();
          		} catch(ParserConfigurationException pce)
          		{
          			System.err.println(pce.getMessage());
          			System.exit(1);
          		}
          		//加載XML文檔
          		Document doc = null;
          		try{
          			doc = db.parse(this.fileName);
          		} catch(DOMException dom)
          		{
          			System.err.println(dom.getMessage());
          			System.exit(1);
          		} catch(Exception ioe)
          		{
          			System.err.println(ioe.getMessage());
          			System.exit(1);
          		}
          		//開始讀取數據
          		Element root = doc.getDocumentElement();
          		//開始top的讀取
          		NodeList top = root.getElementsByTagName("top");
          		
          		for (int i = 0; i < top.getLength(); i++)
          		{
          			Element menu = (Element) top.item(i); 
          			//創建頂層菜單
          			String menuName = menu.getAttribute("name");
          			JMenu topMenu = new JMenu(menuName);
          			this.rootMenu.add(topMenu);
          			
          			NodeList menuChild = menu.getChildNodes();
          			for(int j = 1; j < menuChild.getLength(); j += 2)
          			{
          				//創建次級菜單
          				Element child = (Element) menuChild.item(j);
          				String nodeName = child.getNodeName();
          				if(nodeName.equals("separator"))
          				{
          					//為分隔線
          					topMenu.addSeparator();
          				} else if(nodeName.equals("menu"))
          				{
          					//菜單項
          					NodeList n1 = child.getChildNodes();
          					//菜單標題
          					String str1 = n1.item(0).getNodeValue();
          					//添加菜單
          					JMenuItem mItem = new JMenuItem(str1);
          					topMenu.add(mItem);
          					mItem.addActionListener(this.actionListener);
          				} else if(nodeName.equals("popup"))
          				{
          					//彈出菜單項
          					String str2 = child.getAttribute("name");
          					JMenu popupMenu = new JMenu(str2);
          					//添加彈出菜單
          					topMenu.add(popupMenu);
          					NodeList popup = child.getChildNodes();
          					for(int k = 1; k < popup.getLength(); k += 2)
          					{
          						Element popupChild = (Element) popup.item(k);
          						String popupName = popupChild.getNodeName();
          						if(popupName.equals("separator"))
          						{
          							//為分隔線
          							popupMenu.addSeparator();
          						} else if(popupName.equals("menu"))
          						{
          							//菜單項
          							NodeList n3 = popupChild.getChildNodes();
          							String str3 = n3.item(0).getNodeValue();
          							JMenuItem pItem = new JMenuItem(str3);
          							popupMenu.add(pItem);
          							pItem.addActionListener(this.actionListener);
          						}
          					}
          				}
          			}
          		}
          	}
          }
          
          


          來源:http://gforge.osdn.net.cn/snippet/detail.php?type=snippet&id=9

          posted @ 2006-11-27 13:48 零一的偉大 閱讀(224) | 評論 (0)編輯 收藏

          <2006年11月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆檔案

          文章檔案

          搜索

          最新評論

          主站蜘蛛池模板: 颍上县| 南城县| 电白县| 桐城市| 内丘县| 石台县| 西林县| 肇庆市| 濮阳市| 静乐县| 九龙城区| 凯里市| 林州市| 玉屏| 陆河县| 德清县| 旅游| 嘉善县| 桃园县| 宁都县| 雅江县| 磐石市| 霍山县| 治县。| 大邑县| 肃南| 图片| 阿尔山市| 颍上县| 沛县| 江山市| 昆山市| 隆化县| 张北县| 二手房| 临夏市| 德格县| 伊春市| 翼城县| 饶平县| 汪清县|