xml菜单 java_读取XML来创建菜单

/**

* 标题:制作菜单

*

* 注:通过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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值