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 零一的伟大 阅读(221) | 评论 (0)编辑 收藏

仅列出标题  
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

常用链接

留言簿(1)

随笔档案

文章档案

搜索

最新评论