日期:2014-05-20  浏览次数:20934 次

SWT菜单的显示问题
做了个小界面,想实现在点击登陆菜单项以前只显示:系统和帮助菜单项,点击登陆菜单项以后显示:系统,编辑,申请,查询和帮助菜单项。我用SWTdesigner做的,请高手指点,源代码如下:
  
import org.eclipse.swt.SWT;


import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import com.swtdesigner.SWTResourceManager;


public class client {

protected Shell shell;


/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
client window = new client();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}

/**
* Create contents of the window.
*/
protected void createContents() 
{
//shell = new Shell(SWT.TITLE); 去掉最大化,最小化
shell = new Shell();
shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_DARK_SHADOW));
shell.setSize(450, 300);
shell.setText("欢迎使用");

Menu menu = new Menu(shell, SWT.BAR);
shell.setMenuBar(menu);

final MenuItem system = new MenuItem(menu, SWT.CASCADE);
system.setText("系统(&S)");
Menu menu_1 = new Menu(system);
system.setMenu(menu_1);

MenuItem login = new MenuItem(menu_1, SWT.NONE);
login.setText("登陆");
final MenuItem logout = new MenuItem(menu_1, SWT.NONE);
logout.setText("注销");
logout.setEnabled(false);//不可编辑
final MenuItem exit = new MenuItem(menu_1, SWT.NONE);
exit.setText("退出");

final MenuItem edit= new MenuItem(menu, SWT.CASCADE);
edit.setText("编辑(&E)");
Menu menu_2 = new Menu(edit);
edit.setMenu(menu_2);

MenuItem revocation = new MenuItem(menu_2, SWT.NONE);
revocation.setText("撤销(&U) Ctrl+Z");
MenuItem cut = new MenuItem(menu_2, SWT.NONE);
cut.setText("剪切(&X) Ctrl+X");
MenuItem copy = new MenuItem(menu_2, SWT.NONE);
copy.setText("复制(&C) Ctrl+C");
MenuItem paste = new MenuItem(menu_2, SWT.NONE);
paste.setText("粘贴(&V) Ctrl+V");
edit.dispose(); //不显示


final MenuItem application = new MenuItem(menu, SWT.CASCADE);
application.setText("申请(&I)");
Menu menu_3 = new Menu(application);
application.setMenu(menu_3);
MenuItem app = new MenuItem(menu_3, SWT.NONE);
app.setText("证书申请");
application.dispose(); //不显示


final MenuItem select = new MenuItem(menu, SWT.CASCADE);
select.setText("查询(&R)");
Menu menu_4 = new Menu(select);
select.setMenu(menu_4);
MenuItem menuItem_1 = new MenuItem(menu_4, SWT.NONE);
menuItem_1.setText("证书查询");
select.dispose(); //不显示


final MenuItem help = new MenuItem(menu, SWT.CASCADE);
help.setText("帮助(&H)");
Menu menu_5 = new Menu(help);
help.setMenu(menu_5);

MenuItem about = new MenuItem(menu_5, SWT.NONE);
about.setText("关于Client");
MenuItem use = new MenuItem(menu_5, SWT.NONE);
use.setText("使用介绍");

//*********************菜单响应事件**************************
login.addListener(SWT.Selection, new Listener()