我如何知道哪个菜单项被点击? [英] How do i know that which menu item is clicked?

查看:105
本文介绍了我如何知道哪个菜单项被点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是

  VerticalPanel v1 = new VerticalPanel(); 

命令comm = new Command(){
@Override
public void execute(){
//我如何知道哪个菜单项被cliked
}
};

MenuBar menu = new MenuBar();
menu.setWidth(500px);
menu.setAnimationEnabled(true);
menu.setAutoOpen(true);
menu.addSeparator();
MenuBar fileBar = new MenuBar(true);
MenuBar editBar = new MenuBar(true);

fileBar.addItem(new MenuItem(New,comm));
fileBar.addSeparator();
fileBar.addItem(new MenuItem(Open,comm));
fileBar.addItem(new MenuItem(Save,comm));

editBar.addItem(Edit 11,comm);
editBar.addItem(Edit 11,comm);

menu.addItem(new MenuItem(File,fileBar));
menu.addItem(new MenuItem(Edit,editBar));

v1.add(menu);

请帮助我

解决方案

我似乎不是你开箱即用的东西。但您可以使用以下选项:


  1. MenuBar 有一个受保护的方法 getSelectedItem(),这将返回 MenuItem ,它应该与单击的匹配。我不知道它为什么会受到保护,但通过扩展 MenuBar 类并将其公开,您应该可以使用它。


  2. 您可以创建 Command 类,在创建时注入 MenuItem ,in这种情况下,您需要在创建后设置命令,而不是在 MenuItem


$ b $的构造函数中b

命令实现:

  public class MyCommand implements Command {

private最终的MenuItem项目;

public MyCommand(MenuItem item){
this.item = item;
}

@Override
public void execute(){
//项目匹配被点击的项目。


$ / code $ / pre

用法:

  MenuItem newItem = new MenuItem(New,(Command)null); 
newItem.setCommand(new MyCommand(newItem));

或者不要通过 MenuItem MyCommand 构造函数为 MyCommand 类添加一个方法,该类名为 setMenuItem

  MenuItem newItem = new MenuItem(New,new MyCommand()); 
((MyCommand)newItem.getCommand())。setMenuItem(newItem);


My code is

          VerticalPanel v1 = new VerticalPanel();

    Command comm = new Command() {
        @Override
        public void execute() {
                       // How i know that which menu item is cliked
        }
    };

    MenuBar menu = new MenuBar();
    menu.setWidth("500px");
    menu.setAnimationEnabled(true);
    menu.setAutoOpen(true);
    menu.addSeparator();
    MenuBar fileBar = new MenuBar(true);
    MenuBar editBar = new MenuBar(true);

    fileBar.addItem(new MenuItem("New", comm));
    fileBar.addSeparator();
    fileBar.addItem(new MenuItem("Open", comm));
    fileBar.addItem(new MenuItem("Save", comm));

    editBar.addItem("Edit 11", comm);
    editBar.addItem("Edit 11", comm);

    menu.addItem(new MenuItem("File", fileBar));
    menu.addItem(new MenuItem("Edit", editBar));

    v1.add(menu);

please help me

解决方案

I doesn't seem to be something you get out of the box. But you can use on of the following options:

  1. In MenuBar there is a protected method getSelectedItem(), this returns the MenuItem which should match the one clicked. I don't know why it's protected, but by extending the MenuBar class and make it public you should be able to use it.

  2. You can create a Command class, where you inject the MenuItem upon creation, in that case you need to set the command after creation and not in the constructor of the MenuItem

Command implementation:

public class MyCommand implements Command {

  private final MenuItem item;

  public MyCommand(MenuItem item) {
    this.item = item;
  }

  @Override
  public void execute() {
    //item matches the item clicked.
  }
}

Usage:

MenuItem newItem = new MenuItem("New", (Command)null);
newItem.setCommand(new MyCommand(newItem));

Or instead of passing the MenuItem via the MyCommand constructor add a method to the MyCommand class named setMenuItem:

MenuItem newItem = new MenuItem("New", new MyCommand());
((MyCommand)newItem.getCommand()).setMenuItem(newItem);

这篇关于我如何知道哪个菜单项被点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆