我正在使用 GWT 构建 AJAX Web 应用程序,并且我想使用右键单击来完成各种操作,就像在桌面应用程序中一样。但是,右键单击会生成标准的 Web 上下文菜单,并且永远不会调用 void onClick(ClickEvent event)。有没有人想出如何让它工作?谢谢!
问问题
7964 次
3 回答
7
easy peasy,在 contextmenuhandler 上添加一个侦听器,它将根据用户右键单击的位置显示一个小部件。https://confluence.clazzes.org/pages/viewpage.action?pageId=425996
class MyWidget extends Composite implements ContextMenuHandler {
// just an example, use a meaningful Widget here...
private Widget base;
private PopupPanel contextMenu;
public MyWidget() {
// initialize base widget, etc...
this.contextMenu = new PopupPanel(true);
this.contextMenu.add(new HTML("My Context menu!"));
this.contextMenu.hide();
initWidget(this.base);
// of course it would be better if base would implement HasContextMenuHandlers, but the effect is the same
addDomHandler(this, ContextMenuEvent.getType());
}
public void onContextMenu(ContextMenuEvent event) {
// stop the browser from opening the context menu
event.preventDefault();
event.stopPropagation();
this.contextMenu.setPopupPosition(event.getNativeEvent().getClientX(), event.getNativeEvent().getClientY());
this.contextMenu.show();
}
}
最后,您需要禁用浏览器菜单以完全重载此类上下文菜单。这应该适用于除歌剧之外的所有浏览器。但老实说,这些天谁在使用它 ^ _______ ^
<body oncontextmenu="return false;">
于 2012-08-06T15:00:05.003 回答
4
事实证明你可以通过扩展来做到这一点DeckPanel
。这是一个很好的讨论,以及一个很好的演示,证明它有效。
http://whatwouldnickdo.com/wordpress/370/gwt-right-click-context-menu/
于 2009-09-16T17:43:30.597 回答
1
虽然有很多方法可以做到这一点,但我相信 GWT 团队对此进行了辩论,并决定在 Web 应用程序中启用右键单击是一件坏事,因此有意识地决定不支持它。论点是右键单击应该继续按预期工作(打开主机浏览器的右键单击上下文菜单),并且覆盖它会破坏预期的行为,并且会是不好的做法。虽然我遇到过右键单击上下文菜单通常很有用的情况,但我倾向于同意 GWT 团队的决定。
于 2009-09-17T08:46:59.783 回答