我正在使用GWT构建一个
AJAX Web应用程序,我想使用右键单击各种内容,就像在桌面应用程序中一样.但是,右键单击会生成标准Web上下文菜单,并且永远不会调用void onClick(ClickEvent事件).有没有人想出如何让这个工作?谢谢!
容易腻,在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(); } }
最后,您将要禁用浏览器菜单以完全重载此类上下文菜单.除了opera之外,这应该适用于所有浏览器.但老实说,这几天新人使用了^ _______ ^
<body oncontextmenu="return false;">