SWT:单击工具栏按钮后在工具栏按钮下方显示弹出菜单

发布于 2024-11-14 20:23:36 字数 260 浏览 6 评论 0原文

我想在用户单击工具栏按钮时在该按钮下方显示一个弹出菜单。我已经阅读过有关 ToolItemSWT.DROP_DOWN 样式,但这似乎非常限于根据 此示例。相反,我想显示一个弹出菜单,其中包含复选框和单选按钮菜单项。

I want to show a popup menu below a toolbar button when the user clicks this button. I've read about the SWT.DROP_DOWN style for a ToolItem but this seems very much limited to a simple list of items according to this sample. Instead, I want to show a popup menu with, e.g., checkbox and radio button menu items.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

濫情▎り 2024-11-21 20:23:36

您可以使用样式 SWT.CHECK、SWT.CASCADE、SWT.PUSH、SWT.RADIO、SWT.SEPARATOR 制作 MenuItem
查看 javadoc..

因此,您可以将 swt 菜单“挂起”到工具栏项目上的下拉菜单选择,如下所示

public class Test {

private Shell shell;

public Test() {
    Display display = new Display();
    shell = new Shell(display, SWT.SHELL_TRIM);
    shell.setLayout(new FillLayout(SWT.VERTICAL));
    shell.setSize(50, 100);

    ToolBar toolbar = new ToolBar(shell, SWT.FLAT);
    ToolItem itemDrop = new ToolItem(toolbar, SWT.DROP_DOWN);
    itemDrop.setText("drop menu");

    itemDrop.addSelectionListener(new SelectionAdapter() {

        Menu dropMenu = null;

        @Override
        public void widgetSelected(SelectionEvent e) {
            if(dropMenu == null) {
                dropMenu = new Menu(shell, SWT.POP_UP);
                shell.setMenu(dropMenu);
                MenuItem itemCheck = new MenuItem(dropMenu, SWT.CHECK);
                itemCheck.setText("checkbox");
                MenuItem itemRadio = new MenuItem(dropMenu, SWT.RADIO);
                itemRadio.setText("radio1");
                MenuItem itemRadio2 = new MenuItem(dropMenu, SWT.RADIO);
                itemRadio2.setText("radio2");
            }

            if (e.detail == SWT.ARROW) {
                // Position the menu below and vertically aligned with the the drop down tool button.
                final ToolItem toolItem = (ToolItem) e.widget;
                final ToolBar  toolBar = toolItem.getParent();

                Point point = toolBar.toDisplay(new Point(e.x, e.y));
                dropMenu.setLocation(point.x, point.y);
                dropMenu.setVisible(true);
            } 

        }

    });

    shell.open();

    while(!shell.isDisposed()) {
        if(!display.readAndDispatch()) display.sleep();
    }

    display.dispose();
}

public static void main(String[] args) {
    new Test();
}

}

You can make MenuItem with styles SWT.CHECK, SWT.CASCADE, SWT.PUSH, SWT.RADIO, SWT.SEPARATOR
see javadoc..

So you can "hang" swt menu to selection of dropdown on toolbar item like this

public class Test {

private Shell shell;

public Test() {
    Display display = new Display();
    shell = new Shell(display, SWT.SHELL_TRIM);
    shell.setLayout(new FillLayout(SWT.VERTICAL));
    shell.setSize(50, 100);

    ToolBar toolbar = new ToolBar(shell, SWT.FLAT);
    ToolItem itemDrop = new ToolItem(toolbar, SWT.DROP_DOWN);
    itemDrop.setText("drop menu");

    itemDrop.addSelectionListener(new SelectionAdapter() {

        Menu dropMenu = null;

        @Override
        public void widgetSelected(SelectionEvent e) {
            if(dropMenu == null) {
                dropMenu = new Menu(shell, SWT.POP_UP);
                shell.setMenu(dropMenu);
                MenuItem itemCheck = new MenuItem(dropMenu, SWT.CHECK);
                itemCheck.setText("checkbox");
                MenuItem itemRadio = new MenuItem(dropMenu, SWT.RADIO);
                itemRadio.setText("radio1");
                MenuItem itemRadio2 = new MenuItem(dropMenu, SWT.RADIO);
                itemRadio2.setText("radio2");
            }

            if (e.detail == SWT.ARROW) {
                // Position the menu below and vertically aligned with the the drop down tool button.
                final ToolItem toolItem = (ToolItem) e.widget;
                final ToolBar  toolBar = toolItem.getParent();

                Point point = toolBar.toDisplay(new Point(e.x, e.y));
                dropMenu.setLocation(point.x, point.y);
                dropMenu.setVisible(true);
            } 

        }

    });

    shell.open();

    while(!shell.isDisposed()) {
        if(!display.readAndDispatch()) display.sleep();
    }

    display.dispose();
}

public static void main(String[] args) {
    new Test();
}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文