Draw2d 学习笔记八 GridLayout ToolbarLayout布

系统 1648 0

原文: http://www.cnblogs.com/bjzhanghao/archive/2006/09/05/495747.html

该文章来之“八进制”。

    public class Draw2DLayoutExample {
	static Figure canvas;//Parent figure which uses XYLayout as its layout manager
	static RectangleFigure containerFig;//canvas's only child, which uses ToolbarLayout
	static RectangleFigure innerContainerFig;//containerFig's only child, which uses ToolbarLayout, too
	static RectangleFigure firstGreenFig;//innerContainerFig's first child, which has no layout manager
	static Dimension dimension = new Dimension(40, 20);

	public static void main(String args[]) {
		Shell shell = new Shell();
		shell.setLayout(new GridLayout(1, false));

		//Create control buttons
		Button button = new Button(shell, SWT.PUSH);
		GridData gd = new GridData();
		button.setLayoutData(gd);
		button.setText("Add Red");
		Button button2 = new Button(shell, SWT.PUSH);
		gd = new GridData();
		button2.setLayoutData(gd);
		button2.setText("Add Green");
		Button button3 = new Button(shell, SWT.PUSH);
		gd = new GridData();
		button3.setLayoutData(gd);
		button3.setText("Enlarge Green");

		//Draw2d area
		LightweightSystem lws = new LightweightSystem(shell);

		//The canvas figure which fills right half of shell
		canvas = new Figure();
		canvas.setLayoutManager(new XYLayout());
		lws.setContents(canvas);
		System.out.println(canvas.getLayoutManager());

		//A rectangle figure
		containerFig = new RectangleFigure();
		canvas.add(containerFig);
		canvas.getLayoutManager().setConstraint(containerFig, new Rectangle(120, 10, -1, -1));
		
		ToolbarLayout layout = new ToolbarLayout();
		layout.setVertical(true);
		layout.setSpacing(3);
		layout.setStretchMinorAxis(false);
		containerFig.setLayoutManager(layout);
		containerFig.setBorder(new MarginBorder(5));
		RectangleFigure fig = new RectangleFigure();
		fig.setBackgroundColor(ColorConstants.red);
		fig.setSize(dimension);
		containerFig.add(fig);

		//A inner container figure
		innerContainerFig = new RectangleFigure();
		ToolbarLayout layout2 = new ToolbarLayout();
		layout2.setVertical(false);
		layout2.setSpacing(3);
		layout2.setStretchMinorAxis(false);
		innerContainerFig.setLayoutManager(layout2);
		innerContainerFig.setBorder(new MarginBorder(5));
		containerFig.add(innerContainerFig);

		//The first green figure in innerContainerFig
		firstGreenFig = new RectangleFigure();
		firstGreenFig.setBackgroundColor(ColorConstants.green);
		firstGreenFig.setSize(dimension);
		innerContainerFig.add(firstGreenFig);

		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				RectangleFigure fig = new RectangleFigure();
				fig.setBackgroundColor(ColorConstants.red);
				fig.setPreferredSize(dimension);
				containerFig.add(fig);
			}
		});

		button2.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				RectangleFigure fig = new RectangleFigure();
				fig.setBackgroundColor(ColorConstants.green);
				fig.setPreferredSize(dimension);
				innerContainerFig.add(fig);
			}
		});

		button3.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				//Make this figure bigger, and see if the outer figure grows accordingly
				firstGreenFig.setPreferredSize(100, 100);
			}
		});

		shell.setSize(500, 400);
		shell.open();
		shell.setText("Draw2D Layout Example");
		Display display = Display.getDefault();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
}
  

 
Draw2d 学习笔记八 GridLayout ToolbarLayout布局管理器
 

文中提到了:

Draw2D里Figure类的setPreferredSize(Dimension)和setSize(Dimension)的区别是,setSize()方法不会调用revalidate()方法导致重新layout,而只是调用repaint()对所涉及到的“脏”区域进行重绘(repaint)。setPreferredSize()方法可以约等于setSize()方法+revalidate()方法,因为在Figure对getPreferredSize(int,int)的实现里,若该figure没有任何layoutmanager,则返回当前size:

例如当父图形使用XYLayout,子图形使用ToolbarLayout时,假设在子图形里又增加了子子图形(子图形里的子图形),add()方法会导致revalidate()的调用,这时父图形的xylayout将检查子图形是否具有constraint,如果有并且有至少一个方向为-1,则利用子图形上的ToolbarLayout计算出子图形新的尺寸,这个尺寸是和子图形里包含的子子图形的数目有关的(ToolbarLayout会把每个子图形的宽/高度加起来,加上其中间隔的空间,再考虑图形的边框,返回得到的尺寸)。

 

指出了figure什么时候,调用revalidate()。

Draw2d 学习笔记八 GridLayout ToolbarLayout布局管理器


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论