Java 动态构建菜单
目前,我的程序有一个动态构建的Java 动态构建菜单,java,swing,menu,jtree,Java,Swing,Menu,Jtree,目前,我的程序有一个动态构建的JTree菜单。我向菜单生成器传递一个对象,该对象用字符串数组描述其路径,其中每个元素表示一个节点,然后设置最后一个节点以调用该对象的方法。只有当新节点不存在时,才会创建新节点,否则会选中子菜单并重复整个过程 我试图将其切换到JMenu系统,而不是树,但我遇到了一些问题 例如,字符串数组类似于: ["New", "Job", "Item A"] ["New", "Job", "Item B"] ["New", "Search", "Item C"] 这将创建一棵树
JTree
菜单。我向菜单生成器传递一个对象,该对象用字符串数组描述其路径,其中每个元素表示一个节点,然后设置最后一个节点以调用该对象的方法。只有当新节点不存在时,才会创建新节点,否则会选中子菜单并重复整个过程
我试图将其切换到JMenu
系统,而不是树,但我遇到了一些问题
例如,字符串数组类似于:
["New", "Job", "Item A"]
["New", "Job", "Item B"]
["New", "Search", "Item C"]
这将创建一棵树,如:
┬ New
├──┬ Job
│ ├── Item A
│ └── Item B
└──┬ Search
└── Item C
现在的问题是,我无法判断是否已经存在带有JMenu
的子菜单。如何检查具有所需名称的子菜单是否已存在
下面是一个如何构建JTree
的示例:
String[] nodeNames = pane.getNodeDirectory();
MenuTreeNode targetNode = root;
int i = 0;
if(targetNode.getChildCount() > 0) {
//Search for the first node that doesn't exist in the pane's directory
for(; i < nodeNames.length; ++i) {
Enumeration e = targetNode.children();
while(e.hasMoreElements()) {
MenuTreeNode node = (MenuTreeNode)e.nextElement();
if(node.getName().equals(nodeNames[i])) {
targetNode = node;
break;
}
}
//We've been over all the children and none matched so we need
//to build the rest of the directory.
if(!e.hasMoreElements()) {
++i;
break;
}
}
}
//Build the remainder of the pane's directory
for(; i < nodeNames.length; ++i) {
MenuTreeNode currentNode = new MenuTreeNode(nodeNames[i]);
targetNode.add(currentNode);
targetNode = currentNode;
}
targetNode.setContentPane(pane);
model.nodeStructureChanged(root);
String[]nodeNames=pane.getNodeDirectory();
MenuTreeNode targetNode=根节点;
int i=0;
if(targetNode.getChildCount()>0){
//搜索窗格目录中不存在的第一个节点
对于(;i
好的,我想到了这个。虽然没有经过彻底的测试,但它似乎是有效的
获取菜单或菜单项名称的方法是通过可访问
界面。我假设所有分支的类型都是JMenu
。我不确定这种假设有多安全
String[] nodeNames = pane.getNodeDirectory();
JMenu currentMenu = null;
int i = 0;
for(int m = 0; m < menuBar.getMenuCount(); ++m) {
String name = menuBar.getMenu(m).getAccessibleContext().getAccessibleName();
if(name.equals(nodeNames[i])) {
currentMenu = menuBar.getMenu(m);
break;
}
}
++i;
if(currentMenu != null && currentMenu.getMenuComponentCount() > 0) {
boolean startBuild = false;
//Find the first non existant child
for(; i < nodeNames.length; ++i) {
for(int j = 0; j < currentMenu.getMenuComponentCount(); ++j) {
Component c = currentMenu.getMenuComponent(j);
//If we're not at the leaf node search for a JMenu and if it
//has the correct name set it as the current menu and move
//onto the next node.
if(i < nodeNames.length - 1 && c instanceof JMenu) {
String name = ((Accessible)c).getAccessibleContext().getAccessibleName();
if(name.equals(nodeNames[i])) {
currentMenu = (JMenu)c;
break;
}
//If we're at the leaf node search for a JMenuItem to make
//sure the requested item doesn't already exist
} else if(i == nodeNames.length - 1 && c instanceof JMenuItem) {
String name = ((Accessible)c).getAccessibleContext().getAccessibleName();
if(name.equals(nodeNames[i])) {
return;
}
}
//If we've reached the last component without finding an
//appropriate item start building the menu
if(j == currentMenu.getMenuComponentCount() - 1) {
startBuild = true;
}
}
if(startBuild) {
break;
}
}
} else if(currentMenu == null) {
currentMenu = new JMenu(nodeNames[i]);
menuBar.add(currentMenu);
}
//Continue the loop from where we left off but this time making the
//missing nodes instead of searching for existing nodes.
for(; i < nodeNames.length - 1; ++i) {
JMenu newMenu = new JMenu(nodeNames[i]);
currentMenu.add(newMenu);
currentMenu = newMenu;
}
JMenuItem item = new JMenuItem(nodeNames[nodeNames.length - 1]);
item.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
pane.giveFocus();
}
});
currentMenu.add(item);
String[]nodeNames=pane.getNodeDirectory();
JMenu currentMenu=null;
int i=0;
对于(int m=0;m0){
布尔startBuild=false;
//查找第一个不存在的子项
对于(;i
您可以混合使用一些不同的方法调用和类型检查来完成此任务
下面的代码动态构建菜单,丢弃重复项
public static void main(String[] args) {
// just example input
String[][] menuHierarchies = {
{"New", "Job", "Item A"},
{"New", "Job", "Item B"},
{"New", "Search", "Item C"},
{"New", "Item D"},
{"Other", "Item E"},
{"New", "Job", "Item B"},
{"Other", "Job", "Item B"}
};
JMenuBar menuBar = new JMenuBar();
// relevant code from here. It builds the menu removing redundancies
for (int rootIndex = 0; rootIndex < menuHierarchies.length; ++rootIndex) {
JMenu parentMenu = null;
for(int menuLevel = 0; menuLevel < menuHierarchies[rootIndex].length; ++menuLevel) {
// if it is root menu
if (menuLevel == 0) {
// checks if the root menu already exists
for (int i = 0; i < menuBar.getMenuCount(); ++i) {
if (menuBar.getMenu(i).getText().equals(menuHierarchies[rootIndex][menuLevel])) {
parentMenu = menuBar.getMenu(i);
break;
}
}
if (parentMenu == null) {
parentMenu = new JMenu(menuHierarchies[rootIndex][menuLevel]);
menuBar.add(parentMenu);
}
} else if (menuLevel < menuHierarchies[rootIndex].length - 1) { // if it is a non-leaf (and, of course, not a root menu)
Component[] menuComponents = parentMenu.getMenuComponents();
JMenu currentMenu = null;
// checks if the menu already exists
for (Component component : menuComponents) {
if (component instanceof JMenu) {
if (((JMenu) component).getText().equals(menuHierarchies[rootIndex][menuLevel])) {
currentMenu = (JMenu) component;
break;
}
}
}
if (currentMenu == null) {
currentMenu = new JMenu(menuHierarchies[rootIndex][menuLevel]);
parentMenu.add(currentMenu);
}
parentMenu = currentMenu;
} else { // if it is a leaf
Component[] menuComponents = parentMenu.getMenuComponents();
JMenuItem currentItem = null;
for (Component component : menuComponents) {
if (component instanceof JMenuItem) {
if (((JMenuItem) component).getText().equals(menuHierarchies[rootIndex][menuLevel])) {
currentItem = (JMenuItem) component;
break;
}
}
}
if (currentItem == null) {
parentMenu.add(new JMenuItem(menuHierarchies[rootIndex][menuLevel]));
}
}
}
}
}
publicstaticvoidmain(字符串[]args){
//只是示例输入
字符串[][]菜单层次结构={
{“新建”、“作业”、“项目A”},
{“新建”、“作业”、“B项”},
{“新建”、“搜索”、“C项”},
{“新”、“D项”},
{“其他”、“E项”},
{“新建”、“作业”、“B项”},
{“其他”、“工作”、“B项”}
};
JMenuBar menuBar=新的JMenuBar();
//这里的相关代码。它构建了删除冗余的菜单
for(int rootIndex=0;rootIndexprivate static void printMenu(JMenuBar menuBar) {
for (int i = 0; i < menuBar.getMenuCount(); ++i) {
printMenu(menuBar.getMenu(i), 0);
}
}
private static void printMenu(JMenuItem menuItem, int level) {
for (int i = 0; i < level; ++i) {
System.out.print("\t");
}
System.out.println("\\" + menuItem.getText());
if (menuItem instanceof JMenu) {
JMenu menu = (JMenu) menuItem;
Component[] menuComponents = menu.getMenuComponents();
for (Component component : menuComponents) {
if (component instanceof JMenuItem) {
printMenu((JMenuItem) component, level+1);
}
}
}
}