本文主要是实现了Menu(菜单)、ToolBar(工具条)、CoolBar,这些在平时做界面中都是很常用的,相比以前的简单控件,这些控件的使用更加灵活以及困难。
Menu的实现了其中很多常用的类型,比如单选、复选菜单以及右键菜单,ToolBar实现了单选、复选以及下拉方式,而CoolBar作为一种灵活的控件,你可以在上面放置控件,并且能够实现任意的拖拽。
实现代码:
/**
* @author XiangJie
*
*/
public class Test_MenuAndToolBar {
public void test_MenuAndToolBar(Composite parent) {
show_ControlMenu(parent);
show_ToolBar(parent);
showCoolBar(parent);
}
private void showCoolBar(Composite parent) {
Composite tempCom = new Composite(parent, SWT.BORDER);
tempCom.setLayout(new FillLayout());
CoolBar coolBar = new CoolBar(tempCom, SWT.BORDER);
// coolBar.setLayout(new FillLayout());
CoolItem item1 = new CoolItem(coolBar, SWT.NONE);
Combo combo = new Combo(coolBar, SWT.NONE | SWT.READ_ONLY);
combo.setItems(new String[] { "test1", "test2" });
Point p = combo.computeSize(SWT.DEFAULT, SWT.DEFAULT);
combo.setSize(p);
Point p2 = item1.computeSize(p.x, p.y);
item1.setSize(p2);
item1.setControl(combo);
CoolItem item2 = new CoolItem(coolBar, SWT.NONE);
Button testButton = new Button(coolBar, SWT.NONE);
testButton.setText("Test!");
p = testButton.computeSize(SWT.DEFAULT, SWT.DEFAULT);
combo.setSize(p);
p2 = item2.computeSize(p.x, p.y);
item2.setSize(p2);
item2.setControl(testButton);
}
private void show_ToolBar(Composite parent) {
Composite tempCom = new Composite(parent, SWT.BORDER);
tempCom.setLayout(new FillLayout());
final ToolBar toolBar = new ToolBar(tempCom, SWT.WRAP);
ToolItem pItem = new ToolItem(toolBar, SWT.PUSH);
pItem.setText("Push me!");
new ToolItem(toolBar, SWT.SEPARATOR);
ToolItem cItem1 = new ToolItem(toolBar, SWT.CHECK);
cItem1.setText("Check me1!");
ToolItem cItem2 = new ToolItem(toolBar, SWT.CHECK);
cItem2.setText("Check me2!");
new ToolItem(toolBar, SWT.SEPARATOR);
ToolItem rItem1 = new ToolItem(toolBar, SWT.RADIO);
rItem1.setText("Radio me1!");
ToolItem rItem2 = new ToolItem(toolBar, SWT.RADIO);
rItem2.setText("Radio me2!");
final ToolItem droItem = new ToolItem(toolBar, SWT.DROP_DOWN);
droItem.setText("Drop down me");
final Menu testMenu = new Menu(tempCom.getShell(), SWT.POP_UP);
(new MenuItem(testMenu, SWT.NONE)).setText("Just to Test!");
droItem.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
// TODO Auto-generated method stub
boolean flat = testMenu.getVisible();
if (!flat) {
showMenu();
} else {
testMenu.setVisible(false);
}
}
private void showMenu() {
Rectangle rct = droItem.getBounds();
Point point = toolBar.toDisplay(rct.x, rct.y + rct.height);
testMenu.setLocation(point);
testMenu.setVisible(true);
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
});
}
private void show_ControlMenu(Composite parent) {
Composite tempCom = new Composite(parent, SWT.BORDER);
tempCom.setLayout(new FillLayout());
final Label testLabel = new Label(tempCom, SWT.BORDER | SWT.WRAP);
testLabel.setText("Please use right click!");
testLabel.setBackground(new Color(null, 255, 0, 0));
final Menu menu = new Menu(testLabel);
testLabel.setMenu(menu);
MenuItem item1 = new MenuItem(menu, SWT.NONE);
item1.setText("test!");
menu.addMenuListener(new MenuListener() {
@Override
public void menuShown(MenuEvent e) {
// TODO Auto-generated method stub
// boolean flat = menu.getVisible();
testLabel.setText("Menu is visiable!");
menu.setVisible(true);
}
@Override
public void menuHidden(MenuEvent e) {
// TODO Auto-generated method stub
testLabel.setText("Menu is hidden!");
menu.setVisible(false);
}
});
}
public void shellMenuTest(Shell shell) {
Menu menu = new Menu(shell, SWT.BAR | SWT.DROP_DOWN);
MenuItem item1 = new MenuItem(menu, SWT.CASCADE);
item1.setText("&Open");
Menu suMenu = new Menu(shell, SWT.DROP_DOWN);// new Menu(item1);
item1.setMenu(suMenu);
MenuItem item2 = new MenuItem(suMenu, SWT.RADIO);
item2.setText("&Save");
MenuItem item3 = new MenuItem(suMenu, SWT.RADIO);
item3.setText("&Test");
new MenuItem(suMenu, SWT.SEPARATOR);
MenuItem item4 = new MenuItem(suMenu, SWT.CHECK);
item4.setText("&Check");
MenuItem item5 = new MenuItem(suMenu, SWT.CHECK);
item5.setText("&SCheck");
new MenuItem(suMenu, SWT.SEPARATOR);
MenuItem item6 = new MenuItem(suMenu, SWT.CASCADE);
item6.setText("&Menu");
Menu suMenu2 = new Menu(shell, SWT.DROP_DOWN);// new Menu(item1);
item6.setMenu(suMenu2);
MenuItem item7 = new MenuItem(suMenu2, SWT.CASCADE);
item7.setText("&First");
shell.setMenuBar(menu);
}
/**
* create data: 2010-1-14</br>
*
* @author XiangJie
* @param args
*
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("SWT Menu and ToolBar:");
shell.setLayout(new FillLayout(SWT.VERTICAL));
Test_MenuAndToolBar test = new Test_MenuAndToolBar();
test.shellMenuTest(shell);
test.test_MenuAndToolBar(shell);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
效果演示:
本文系eclipselight.org(日食之光)原创文章,转载请注明出处。