【发布时间】:2012-09-05 02:18:18
【问题描述】:
当按下加速键组合时,具有 SWT.CASCADE 样式(如“文件”)的 MenuItem 对象不会下拉。
在下面的示例中,按 Alt-F 会触发选择事件(我在控制台中看到“文件”),但菜单本身不会下拉。我也找不到以编程方式使菜单下拉的方法。有什么想法吗?
(我使用的包是 org.eclipse.swt.win32.win32.x86_64_3.100.0.v4233d.jar 随当前版本的 Eclipse Juno 提供。)
public class MenuTest {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Menu menu = new Menu(shell, SWT.BAR);
MenuItem item = new MenuItem(menu, SWT.CASCADE);
item.setText("File");
item.setAccelerator(SWT.ALT | 'F');
Menu dropMenu = new Menu(shell, SWT.DROP_DOWN);
item.setMenu(dropMenu);
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
System.out.println("File");
}
});
item = new MenuItem(dropMenu, SWT.NULL);
item.setText("Close");
item.setAccelerator(SWT.ALT | SWT.F4);
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
System.out.println("Close");
shell.dispose();
}
});
shell.setMenuBar(menu);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
【问题讨论】:
-
@nick- SWT.CASCADE 对我有用,尽管我在
org.eclipse.swt.win32.win32.x86_3.100.0.v4233d.jar上。你能发布一个运行的例子吗? -
加速器似乎可以工作,但菜单不会下拉。我重新表述了这个问题并添加了一个示例。