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))代码>,如果你喜欢那种单线编程。