簡體   English   中英

如何使用本機GWT(Java)實現上下文菜單?

[英]How to implement context menu with native GWT (Java)?

我是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 );

但它不起作用。 謝謝。

只是寫了一些步驟,為我完成了我的工作代碼。

GWT中的上下文菜單或右鍵單擊處理程序

這是該代碼的基本部分。

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()
);

event.getNativeEvent()。getClientX()和event.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;
}-*/;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM