Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.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
SWT:单击工具栏按钮后在其下方显示弹出菜单_Swt_Toolbar_Popupmenu - Fatal编程技术网

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

SWT:单击工具栏按钮后在其下方显示弹出菜单,swt,toolbar,popupmenu,Swt,Toolbar,Popupmenu,当用户单击工具栏按钮时,我想在其下方显示一个弹出菜单。我读过关于ToolItem的SWT.DROP_DOWN样式的文章,但这似乎仅限于一个简单的项目列表。相反,我想显示一个带有复选框和单选按钮菜单项的弹出菜单。您可以使用SWT.CHECK、SWT.CASCADE、SWT.PUSH、SWT.radio、SWT.SEPARATOR等样式创建菜单项 所以你们可以像这样“挂起”swt菜单来选择工具栏上的下拉菜单项 public class Test { private Shell shell; p

当用户单击工具栏按钮时,我想在其下方显示一个弹出菜单。我读过关于
ToolItem
SWT.DROP_DOWN
样式的文章,但这似乎仅限于一个简单的项目列表。相反,我想显示一个带有复选框和单选按钮菜单项的弹出菜单。

您可以使用SWT.CHECK、SWT.CASCADE、SWT.PUSH、SWT.radio、SWT.SEPARATOR等样式创建菜单项

所以你们可以像这样“挂起”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();
}

}

顺便说一句,
SWT.DROP\u DOWN
工具栏按钮是否可以区分单击普通按钮(应直接调用操作)和单击下拉区域(应显示下拉菜单)?是的。作为listener方法参数获得的
SelectionEvent e
具有属性
e.detail
,如果按下按钮,该属性为
0
,如果按下箭头,该属性为
4
(即
SWT.ARROW
)值得注意的是
setLocation()
方法也可以接受
,可以执行
dropMenu.setLocation(toolBar.toDisplay)(新点(e.x,e.y)),如果你喜欢那种单线编程。