- 介绍
菜单(Menu类、MenuItem类)是常用的SWT组件,Menu是一个菜单栏,也是一个容器,可以容纳MenuItem。Menu的容器作用主要体现在以下两个方面:
1.作为整个菜单体系的容器。
2.作为同一菜单栏下多级菜单的分割容器。
在下面的代码中您将认识到这一点。
Menu式样
SWT.BAR : 菜单栏,用于主菜单。
SWT.DROP_DOWN : 下拉菜单用于子菜单。
SWT.POP_UP : 鼠标右键弹出式菜单。
MenuItem式样
SWT.CASCADE : 有子菜单的菜单项。
SWT.CHECK : 选中后前面带小钩。
SWT.PUSH : 普通型菜单项。
SWT.RADIO : 选中后前面带一小点。
SWT.SEPERATOR : ---- 分隔符.
演示代码
public class MethodUtil {
public void buildMenus(){
Display display = new Display();
Shell shell = new Shell(display);
Label label = new Label(shell,SWT.BORDER|SWT.RIGHT);
label.setBounds(10, 10, 120, 140);
label.setText("这里是标签");
label.setFont(new Font(display,"宋体",14,SWT.BOLD));
label.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));
//这里指定是主菜单,且菜单方向是从右到左排列
Menu mainMenu = createMenu(shell, SWT.BAR|SWT.RIGHT_TO_LEFT);
shell.setMenuBar(mainMenu);
MenuItem fileMenuItem = createMenuItem(mainMenu, SWT.CASCADE, "文件&F", null, SWT.CONTROL+'f', true, null);
Menu fileMenu = createMenu(shell, SWT.DROP_DOWN, fileMenuItem, true);
MenuItem createItem = createMenuItem(fileMenu, SWT.CASCADE, "新建&C", null, SWT.CONTROL+'c', true, null);
Menu createMenu = createMenu(shell, SWT.DROP_DOWN, createItem, true);
/*采用反射机制调用调用事件处理方法*/
Class[] c = new Class[1];
Object[] o = new Object[1];
c[0] = Label.class;
o[0] = label;
MenuItem createProject = createMenuItem(createMenu, SWT.PUSH, "项目\tControl+Shit+N", null, SWT.CONTROL+SWT.SHIFT+'N', true, "changeContent",c,o);
MenuItem createPackege = this.createMenuItem(createMenu, SWT.PUSH, "包&I", null, SWT.CONTROL+'I', true, null);
MenuItem createClass = this.createMenuItem(createMenu, SWT.PUSH, "类&K", null, SWT.CONTROL+'K', true, null);
MenuItem open = this.createMenuItem(fileMenu, SWT.PUSH, "打开&S", null, SWT.CONTROL+'S', true, null);
MenuItem exit = this.createMenuItem(fileMenu, SWT.PUSH, "关闭&S", null, SWT.CONTROL+'M', true, null);
MenuItem help = this.createMenuItem(mainMenu, SWT.PUSH, "帮助&H", null, SWT.CONTROL+'H', true, null);
shell.pack();
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
public void changeContent(Label label){
label.setText("我变,我变,我变变变!创建工程!");
}
protected Menu createMenu(Menu parent, boolean enabled) {
Menu m = new Menu(parent);
m.setEnabled(enabled);
return m;
}
protected Menu createMenu(MenuItem parent, boolean enabled) {
Menu m = new Menu(parent);
m.setEnabled(enabled);
return m;
}
protected Menu createMenu(Shell parent, int style) {
Menu m = new Menu(parent, style);
return m;
}
protected Menu createMenu(Shell parent, int style,
MenuItem container, boolean enabled) {
Menu m = createMenu(parent, style);
m.setEnabled(enabled);
container.setMenu(m);
return m;
}
protected Menu createPopupMenu(Shell shell) {
Menu m = new Menu(shell, SWT.POP_UP);
shell.setMenu(m);
return m;
}
protected Menu createPopupMenu(Shell shell, Control owner) {
Menu m = createPopupMenu(shell);
owner.setMenu(m);
return m;
}
protected MenuItem createMenuItem(Menu parent, int style, String text,
Image icon, int accel, boolean enabled,
String callback,Class[] c,Object[] o){
MenuItem mi = new MenuItem(parent, style);
if (text != null) {
mi.setText(text);
}
if (icon != null) {
mi.setImage(icon);
}
if (accel != -1) {
mi.setAccelerator(accel);
}
mi.setEnabled(enabled);
if (callback != null) {
registerCallback(mi, this, callback,c,o);
}
return mi;
}
protected MenuItem createMenuItem(Menu parent, int style, String text,
Image icon, int accel, boolean enabled,
String callback) {
MenuItem mi = new MenuItem(parent, style);
if (text != null) {
mi.setText(text);
}
if (icon != null) {
mi.setImage(icon);
}
if (accel != -1) {
mi.setAccelerator(accel);
}
mi.setEnabled(enabled);
if (callback != null) {
registerCallback(mi, this, callback);
}
return mi;
}
protected void registerCallback(final MenuItem mi,
final Object handler,
final String handlerName) {
mi.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
try {
Method m = handler.getClass().getMethod(handlerName, null);
m.invoke(handler, null);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
});
}
protected void registerCallback(final MenuItem mi,final Object handler,final String handlerName,final Class[] cs,final Object[] params){
mi.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
try {
Method m = handler.getClass().getMethod(handlerName, cs);
m.invoke(handler, params);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
});
}
}
/*调用测试*/
public class MenuTestDesign {
public static void main(String[] arg){
MethodUtil u = new MethodUtil();
u.buildMenus();
}
}
运行结果:
posted on 2009-08-13 17:01
zhangxl 阅读(1157)
评论(0) 编辑 收藏 所属分类:
SWT、SWING、AWT