我使用JFace ApplicationWindow创建了一个简单的窗口,并尝试添加菜单栏。
public MainWindow() {
super(null);
this.addMenuBar(); // Enable menu-bar.
}
@Override
protected Control createContents(Composite parent) {
MenuManager menuBar = this.getMenuBarManager();
// Create "File" menu.
MenuManager fileMenu = new MenuManager("&File");
menuBar.add(fileMenu);
// Create "File" > "Test" action.
Action testAction = new Action("&Test") {
@Override
public void run() {
System.out.println("Test");
}
};
fileMenu.add(testAction);
// Create contents.
Label text = new Label(parent, SWT.NONE);
text.setText("Lorem ipsum.");
return parent;
}
我必须忽略一些简单的东西,因为它似乎不起作用。菜单栏根本不显示。我究竟做错了什么? (请注意,我在Ubuntu中禁用了全局菜单。)
注意:我已经在Ubuntu 15.10上使用GTK 3.16.7和Arch Linux使用GTK 3.22.18进行了测试,包括SWT 3.105.3和JFace 3.12.2。
答案 0 :(得分:1)
在createContents
中设置菜单栏为时已晚,您需要提前执行此操作。一种方法是覆盖createMenuManager
:
@Override
protected MenuManager createMenuManager()
{
MenuManager menuBar = new MenuManager();
// Create "File" menu.
MenuManager fileMenu = new MenuManager("File");
menuBar.add(fileMenu);
// Create "File" > "Test" action.
Action testAction = new Action("&Test")
{
@Override
public void run()
{
System.out.println("Test");
}
};
fileMenu.add(testAction);
return menuBar;
}
@Override
protected Control createContents(Composite parent)
{
// Create contents.
Label text = new Label(parent, SWT.NONE);
text.setText("Lorem ipsum.");
return text;
}