问题描述
我的代码是
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);
请帮助我
我似乎不是你开箱即用的东西。但您可以使用以下选项:
-
在
MenuBar
有一个受保护的方法getSelectedItem()
,这将返回MenuItem
,它应该与单击的匹配。我不知道它为什么会受到保护,但通过扩展MenuBar
类并将其公开,您应该可以使用它。 -
您可以创建
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:
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.
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);
这篇关于我如何知道哪个菜单项被点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!