讀取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