CoolBar Tutorial - ecsoya/eclipse.tutorial GitHub Wiki

CoolBarToolBar有点类似,一般都是用在创建工具栏上,不过,CoolBar的功能更加丰富,CoolItem可以在CoolBar上面自由的拖拽,隐藏等等。它是由org.eclipse.swt.widgets.CoolBarorg.eclipse.swt.widgets.CoolItem实现的。

我们先看一个简单的示例吧:

	Display display = new Display();
	final Shell shell = new Shell(display);

	shell.setText("CoolBar Tutorial");
	shell.setSize(300, 200);

	shell.setLayout(new RowLayout());

	final Image taskImage = new Image(display,
			CoolBarTutorial.class.getResourceAsStream("task.gif"));

	final CoolBar coolBar = new CoolBar(shell, SWT.HORIZONTAL | SWT.FLAT);

	for (int i = 0; i < 5; i++) {
		CoolItem item = new CoolItem(coolBar, SWT.NONE);
		Button control = new Button(coolBar, SWT.NONE);
		control.setImage(taskImage);
		control.setText("Button - " + i);
		item.setControl(control);
		Point size = control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		item.setPreferredSize(item.computeSize(size.x, size.y));
	}
	coolBar.setWrapIndices(new int[] { 1, 3 });
	coolBar.pack();
	shell.open();

	while (!shell.isDisposed()) {
		if (!display.readAndDispatch()) {
			display.sleep();
		}
	}
	taskImage.dispose();
	display.dispose();

如图:

images/image_swt_coolbar1.png

要点

CoolBar

  1. OrientationCoolBar也分为水平的(SWT.HORIZONTAL)和垂直(SWT.VERTICAL)的两种。
  2. SWT.FLAT没有设置的话,CoolBar会有带系统默认的背景色,否则没有。
  3. setLock()设置CoolItem是否锁定,默认为不锁定,如上述示例中的CoolItem就可以拖拽移动;一旦锁定,就不能移动。
  4. setWrapIndices()设置要换行的CoolItem的索引值,默认会将所有的CoolItem显示在同一行。如上述示例中设置了new int[] { 1, 3 },就是说遇到第1个和第3个CoolItem的时候换行显示。

CoolItem

  1. SizeCoolItem的大小一定要自己设置,其中有三个概念,setMinimumSize(),setPreferredSize()和setSize(),并且这些值的默认都是0。最小值是在缩放CoolItem的时候会用到,size如果没有设置,就取PreferredSize,如果都没有设置,则CoolItem就不会显示。
  2. SWT.DROP_DOWN下拉菜单。
  3. SelectionListener选择下拉菜单按钮时触发。

关于2和3,请看示例:drop-down a chevron menu containing hidden tool items


参考资料:


上一篇:ToolBar Tutorial 下一篇:ProgressBar Tutorial