如何从自定义树创建 JPopupMenu

发布于 2025-01-08 19:40:48 字数 1885 浏览 4 评论 0原文

下面的方法从字符串创建自定义数据树。

我正在研究一种生成带有许多子菜单的弹出菜单的方法。基本上这些菜单是动态给出的,所以我有一个算法来处理使用下面的字符串创建子菜单。为了做到这一点,我通过使用分隔符分割来将该字符串转换为 Java 自定义树。

public class MenuItem {

    public String Name;
    public Vector<MenuItem> Childeren;


    public MenuItem() {
        Name = "";
        Childeren = new Vector<MenuItem>();
    }

    public MenuItem(String name) {
        this();
        Name = name;
    }


    public String toString() {
        return Name + " " + Childeren ;
    }
}


public static int createNode(StringTokenizer p_jTokenizer, MenuItem p_iParent) {
    int nCount = 0;

    while(p_jTokenizer.hasMoreTokens()) {
        String strToken = p_jTokenizer.nextToken();

        MenuItem iItem = new MenuItem();

        if(strToken.endsWith("[")) {           
            strToken = strToken.substring(0, strToken.length() - 1);
            nCount =  createNode(p_jTokenizer, iItem);
        }           

        while(strToken.endsWith("]")) {
            nCount++;
            strToken = strToken.substring(0, strToken.length() - 1);               
        }

        iItem.Name = strToken;           
        p_iParent.Childeren.add(iItem);

        while(nCount > 0) {
            return --nCount;
        }
    }
    return nCount;
}

我解析的动态字符串值的示例:

String str = "Menu1;Menu2[;Menu2A;Menu2B[;Menu2B-A;Menu2B-B]];Menu3;";

当前的方法创建一个如下结构的树:

                             Pop-up Menu
                             /    |     \
                       Menu1    Menu2   Menu3
                             /     | 

                          Menu2A     Menu2B
                                      |       \
                                      |        \
                                   Menu2B-A  Menu2B-B

我被困在如何基于该树创建带有子菜单的 JPopUpMenu 上。我想到使用递归方式,但不确定该走哪条路。

The method below creates Custom Data Tree from A String.

I am working on a method that generates pop-up menu with many submenus. Basically these menus given dynamically so I have an algorithm to handle creating submenus using below string. In order to do it I transform this string into Java Custom Tree by splitting using the delimiter.

public class MenuItem {

    public String Name;
    public Vector<MenuItem> Childeren;


    public MenuItem() {
        Name = "";
        Childeren = new Vector<MenuItem>();
    }

    public MenuItem(String name) {
        this();
        Name = name;
    }


    public String toString() {
        return Name + " " + Childeren ;
    }
}


public static int createNode(StringTokenizer p_jTokenizer, MenuItem p_iParent) {
    int nCount = 0;

    while(p_jTokenizer.hasMoreTokens()) {
        String strToken = p_jTokenizer.nextToken();

        MenuItem iItem = new MenuItem();

        if(strToken.endsWith("[")) {           
            strToken = strToken.substring(0, strToken.length() - 1);
            nCount =  createNode(p_jTokenizer, iItem);
        }           

        while(strToken.endsWith("]")) {
            nCount++;
            strToken = strToken.substring(0, strToken.length() - 1);               
        }

        iItem.Name = strToken;           
        p_iParent.Childeren.add(iItem);

        while(nCount > 0) {
            return --nCount;
        }
    }
    return nCount;
}

An Example of Dynamic String Values that I parse:

String str = "Menu1;Menu2[;Menu2A;Menu2B[;Menu2B-A;Menu2B-B]];Menu3;";

The current method creates a tree like below structure:

                             Pop-up Menu
                             /    |     \
                       Menu1    Menu2   Menu3
                             /     | 

                          Menu2A     Menu2B
                                      |       \
                                      |        \
                                   Menu2B-A  Menu2B-B

I am stuck on how to create JPopUpMenu with submenus based on this tree. I think of using recursive way but not sure which way to go.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我为君王 2025-01-15 19:40:48

遍历树结构。如果一个元素是节点,则创建 JMenu;如果它是叶元素,则创建 JMenuItem,将所有子元素添加到 JMenu。

Iterate through the tree structure. If an element is node create JMenu if it's leaf create JMenuItem add all the children to the JMenu.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文