GWT菜单栏上的getSelectedItem()

GWT菜单栏上的getSelectedItem(),gwt,menuitem,menubar,Gwt,Menuitem,Menubar,我是GWT的新手,请帮帮我。我的GWT菜单栏中有一个关于如何getSelectedItem()的问题 这是我的密码: public class Menu extends Composite implements Command { private MenuBar menu = new MenuBar(); private MenuBar priceMgt = new MenuBar( true ); private MenuBar sa

我是GWT的新手,请帮帮我。我的
GWT
菜单栏中有一个关于如何
getSelectedItem()
的问题

这是我的密码:

public class Menu extends Composite implements Command {
    private MenuBar menu            = new MenuBar();
    private MenuBar priceMgt        = new MenuBar( true );
    private MenuBar salesReport     = new MenuBar( true );
    // and a lot more menubars

    private String[] itemPriceMgt = { 
        "Manage Price List", "Print Product Pricing", "Price Proof List"
    };

    private String[] itemSalesReport = { 
        "Sales Transaction List", "Cashier Report", "Media Tender Report",
        "Sales Report by Employee", "Sales Warranty Tracking", "Sales Report by Product", 
        "Sales Summary by Branch", "Sales Summary by Product", "Sales Summary by Period",
        "Product Movement Analysis", "Sales Comparison", 
        "Sales Book", "Download eSales" 
    };


    public Menu() {
        loadMenu();
    }

    private void loadMenu() {
        addSubMenuItem( priceMgt, itemPriceMgt );
        addSubMenuItem( salesReport, itemSalesReport );

        menu.addItem( "Home", false, this );
        menu.addItem( "Price Management", priceMgt );
        menu.addItem( "Sales Report", salesReport );
        menu.addItem( "Logout", false, this );

        initWidget( menu );
    }

    private void addSubMenuItem( MenuBar menubar, String[] list ) {
        for( int i = 0; i < list.length; i++ ) {
            menubar.addItem( list[i], this );
        }
    }

    public void execute() {
        // load the selected module
        // by getting the selected item
        // i can't use menu.getSelectedItem() - it's protected.
        // when i extend this class to MenuBar, adding Item says ambiguous 
        // method addItem(String,Command) for MenuBar
        // how can I get the items being selected/clicked?
    }
}
public类菜单扩展复合实现命令{
私有菜单栏菜单=新建菜单栏();
私有菜单栏价格管理=新菜单栏(真);
私有菜单栏salesReport=新菜单栏(true);
//还有更多的菜单栏
私有字符串[]itemPriceMgt={
“管理价目表”、“打印产品定价”、“价格证明表”
};
私有字符串[]itemSalesReport={
“销售交易清单”、“出纳报告”、“媒体招标报告”,
“按员工列出的销售报告”、“销售保修跟踪”、“按产品列出的销售报告”,
“分公司销售汇总”、“产品销售汇总”、“期间销售汇总”,
“产品运动分析”、“销售比较”,
“销售手册”,“下载电子销售”
};
公共菜单(){
loadMenu();
}
私有void加载菜单(){
添加子菜单项(价格管理,项目价格管理);
添加子菜单项(salesReport、itemSalesReport);
menu.addItem(“Home”,false,this);
菜单.附件(“价格管理”,价格管理);
菜单.addItem(“销售报告”,salesReport);
menu.addItem(“注销”,false,this);
初始化小部件(菜单);
}
private void addSubnumItem(菜单栏菜单栏,字符串[]列表){
for(int i=0;i

其他人可能会说这是一篇没有用的帖子,但我真的不知道该如何理解。请帮帮我。提前感谢。

不要让此类实现
命令
,也不要让所有菜单项在添加
.addItem()
时都使用
作为参数,而是为每个菜单项创建不同的
命令
对象。然后,将每个菜单项的特定逻辑编码到单独的命令对象中

i、 e:


如果您真的想处理同一段代码中的每个菜单项,那么您可以创建一个工厂,该工厂采用菜单项的名称,并生成一个可以引用它的
命令
对象。

这是一个可能适合您的版本。诀窍在于需要将特定命令与每个MenuItem关联,而不是一个命令用于所有MenuItem:

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.MenuBar;

public class Menu extends Composite {
    private MenuBar menu = new MenuBar();
    private MenuBar priceMgt = new MenuBar(true);
    private MenuBar salesReport = new MenuBar(true);
    // and a lot more menubars

    private String[] itemPriceMgt = { "Manage Price List",
            "Print Product Pricing", "Price Proof List" };

    private String[] itemSalesReport = { "Sales Transaction List",
            "Cashier Report", "Media Tender Report",
            "Sales Report by Employee", "Sales Warranty Tracking",
            "Sales Report by Product", "Sales Summary by Branch",
            "Sales Summary by Product", "Sales Summary by Period",
            "Product Movement Analysis", "Sales Comparison", "Sales Book",
            "Download eSales" };

    public Menu() {
        loadMenu();
    }

    private void loadMenu() {
        addSubMenuItem(priceMgt, itemPriceMgt);
        addSubMenuItem(salesReport, itemSalesReport);

        menu.addItem("Home", false, new Command() {
            @Override
            public void execute() {
                // TODO execute a Home command
            }
        });
        menu.addItem("Price Management", new Command() {
            @Override
            public void execute() {
                // TODO execute a Price Management command
            }
        });
        menu.addItem("Sales Report", new Command() {
            @Override
            public void execute() {
                // TODO execute a Sales Report command
            }
        });
        menu.addItem("Logout", false, new Command() {
            @Override
            public void execute() {
                // TODO execute a Logout command
            }
        });

        initWidget(menu);
    }

    class SubMenuItemCommand {
        private String commandName = null;

        public SubMenuItemCommand(String commandName) {
            this.commandName = commandName;
        }

        public Command subMenuItemCommand = new Command() {
            @Override
            public void execute() {
                if (commandName.equals("Manage Price List")) {
                    // TODO execute the Manage Price List command
                } else if (commandName.equals("Print Product Pricing")) {
                    // TODO execute the Print Product Pricing command
                }
                // and so on
            }
        };
    }

    private void addSubMenuItem(MenuBar menubar, String[] list) {
        for (int i = 0; i < list.length; i++) {
            menubar.addItem(list[i],
                    new SubMenuItemCommand(list[i]).subMenuItemCommand);
        }
    }
}
import com.google.gwt.user.client.Command;
导入com.google.gwt.user.client.ui.Composite;
导入com.google.gwt.user.client.ui.MenuBar;
公共类菜单扩展了复合{
私有菜单栏菜单=新建菜单栏();
私有菜单栏价格管理=新菜单栏(真);
私有菜单栏salesReport=新菜单栏(true);
//还有更多的菜单栏
私有字符串[]itemPriceMgt={“管理价目表”,
“打印产品定价”、“价格证明表”};
私有字符串[]itemSalesReport={“销售交易列表”,
“出纳报告”、“媒体招标报告”,
“员工销售报告”、“销售保修跟踪”,
“按产品列出的销售报告”、“按分支机构列出的销售摘要”,
“按产品列出的销售摘要”、“按期间列出的销售摘要”,
“产品运动分析”、“销售比较”、“销售手册”,
“下载eSales”};
公共菜单(){
loadMenu();
}
私有void加载菜单(){
添加子菜单项(价格管理,项目价格管理);
添加子菜单项(salesReport、itemSalesReport);
menu.addItem(“Home”,false,new命令(){
@凌驾
public void execute(){
//TODO执行Home命令
}
});
menu.addItem(“价格管理”,新命令(){
@凌驾
public void execute(){
//TODO执行价格管理命令
}
});
menu.addItem(“销售报告”,new命令()){
@凌驾
public void execute(){
//TODO执行销售报告命令
}
});
menu.addItem(“注销”,false,new命令(){
@凌驾
public void execute(){
//TODO执行注销命令
}
});
初始化小部件(菜单);
}
类子菜单项命令{
私有字符串commandName=null;
公共子菜单项命令(字符串命令名){
this.commandName=commandName;
}
公共命令子菜单项命令=新命令(){
@凌驾
public void execute(){
if(commandName.equals(“管理价目表”)){
//TODO执行管理价目表命令
}else if(commandName.equals(“打印产品定价”)){
//TODO执行“打印产品定价”命令
}
//等等
}
};
}
private void addSubnumItem(菜单栏菜单栏,字符串[]列表){
for(int i=0;i
我已经重新编写了您的代码以使用这种方法,但我已经尝试尽可能地保持与原始设计的接近。其他人可能会建议重写整个内容,但我认为如果我们密切关注您的设计,您将能够更好地理解这是如何工作的

这种设计的一个丑陋的部分是subnumitemcommand类。我为每个子菜单项创建一个单独的对象
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.MenuBar;

public class Menu extends Composite {
    private MenuBar menu = new MenuBar();
    private MenuBar priceMgt = new MenuBar(true);
    private MenuBar salesReport = new MenuBar(true);
    // and a lot more menubars

    private String[] itemPriceMgt = { "Manage Price List",
            "Print Product Pricing", "Price Proof List" };

    private String[] itemSalesReport = { "Sales Transaction List",
            "Cashier Report", "Media Tender Report",
            "Sales Report by Employee", "Sales Warranty Tracking",
            "Sales Report by Product", "Sales Summary by Branch",
            "Sales Summary by Product", "Sales Summary by Period",
            "Product Movement Analysis", "Sales Comparison", "Sales Book",
            "Download eSales" };

    public Menu() {
        loadMenu();
    }

    private void loadMenu() {
        addSubMenuItem(priceMgt, itemPriceMgt);
        addSubMenuItem(salesReport, itemSalesReport);

        menu.addItem("Home", false, new Command() {
            @Override
            public void execute() {
                // TODO execute a Home command
            }
        });
        menu.addItem("Price Management", new Command() {
            @Override
            public void execute() {
                // TODO execute a Price Management command
            }
        });
        menu.addItem("Sales Report", new Command() {
            @Override
            public void execute() {
                // TODO execute a Sales Report command
            }
        });
        menu.addItem("Logout", false, new Command() {
            @Override
            public void execute() {
                // TODO execute a Logout command
            }
        });

        initWidget(menu);
    }

    class SubMenuItemCommand {
        private String commandName = null;

        public SubMenuItemCommand(String commandName) {
            this.commandName = commandName;
        }

        public Command subMenuItemCommand = new Command() {
            @Override
            public void execute() {
                if (commandName.equals("Manage Price List")) {
                    // TODO execute the Manage Price List command
                } else if (commandName.equals("Print Product Pricing")) {
                    // TODO execute the Print Product Pricing command
                }
                // and so on
            }
        };
    }

    private void addSubMenuItem(MenuBar menubar, String[] list) {
        for (int i = 0; i < list.length; i++) {
            menubar.addItem(list[i],
                    new SubMenuItemCommand(list[i]).subMenuItemCommand);
        }
    }
}