Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用原生GWT(Java)实现上下文菜单?_Java_Gwt_Contextmenu - Fatal编程技术网

如何用原生GWT(Java)实现上下文菜单?

如何用原生GWT(Java)实现上下文菜单?,java,gwt,contextmenu,Java,Gwt,Contextmenu,我是GWT的新手! 有人能帮我用GWT实现上下文菜单吗? 我见过一些例子,例如: MenuBar options = new MenuBar(true); MenuBar gwtPopup = new MenuBar(true); options.addItem("GWT", gwtPopup ); MenuItem entryPoint = new MenuItem(new SafeHtmlBuilder().appendEscaped("Ent

我是GWT的新手! 有人能帮我用GWT实现上下文菜单吗? 我见过一些例子,例如:

MenuBar options = new MenuBar(true);
        MenuBar gwtPopup = new MenuBar(true);
        options.addItem("GWT", gwtPopup );
        MenuItem entryPoint = new MenuItem(new SafeHtmlBuilder().appendEscaped("EntryPoint").toSafeHtml());
        entryPoint.setScheduledCommand(new ScheduledCommand()
        {
            public void execute()
            {
                Window.alert( "hello" );
            }
        } );
        final DialogBox menuWrapper = new DialogBox( true );
        menuWrapper.add( options );
        gwtPopup.addItem( entryPoint );
        Button showMenu = new Button( "Click me", new ClickHandler()
        {
            public void onClick( ClickEvent event )
            {
                menuWrapper.showRelativeTo( menuWrapper );
            }
        } );

        RootPanel.get().add( showMenu );

但它不起作用。谢谢。

刚刚在博客上为您介绍了使用我的工作代码完成这项工作的步骤

下面是该代码的基本部分

lable.sinkEvents(Event.ONCONTEXTMENU);
lable.addHandler(
    new ContextMenuHandler() {
        @Override
        public void onContextMenu(ContextMenuEvent event) {
            event.preventDefault();
            event.stopPropagation();
            popupMenu.setPopupPosition(
                event.getNativeEvent().getClientX(),
                event.getNativeEvent().getClientY());
            popupMenu.show();
        }
    }, ContextMenuEvent.getType()
);
事件.getNativeEvent().getClientX()和事件.getNativeEvent().getClientY()在滚动页面后无法正常工作

以下是解决方案:

@Override
public void onContextMenu(ContextMenuEvent event) {
    NativeEvent nativeEvent = event.getNativeEvent();

    // stop the browser from opening the context menu
    event.preventDefault();
    event.stopPropagation();

    int x = nativeEvent.getClientX() + getScrollLeft();
    int y = nativeEvent.getClientY() + getScrollTop();

    this.contextMenu.setPopupPosition(x, y);
    this.contextMenu.show();
}

public static native int getScrollTop() /*-{
return $doc.body.scrollTop;
}-*/;

public static native int getScrollLeft() /*-{
return $doc.body.scrollLeft;
}-*/;

你必须更具体一些。什么不起作用?您希望它做什么?如果您定义代码中哪一位不起作用,这将是一件好事,另一件事是尝试使用PopupPanel而不是dialogJust Blog它为您:)