gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:32:00 26 4
gpt4 key购买 nike

我想在用户单击此按钮时在工具栏按钮下方显示一个弹出式菜单。我读过有关 ToolItemSWT.DROP_DOWN 样式,但这似乎非常限于根据 this sample 的简单项目列表。 .相反,我想显示一个弹出菜单,例如包含复选框和单选按钮菜单项。

最佳答案

您可以使用 SWT.CHECK、SWT.CASCADE、SWT.PUSH、SWT.RADIO、SWT.SEPARATOR 样式制作 MenuItem see 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();
}

}

关于SWT:单击后在工具栏按钮下方显示弹出菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6294763/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com