[Eclipse RCP] 请教:这个布局怎么弄?

liuzhiyuan 2011-03-12

怎么能让“生日”和“性别”对齐?

 

图如下:

布局

 

 

 

 

 

 

 

 

 

 

代码如下:

this.setLayout(new GridLayout(3, false));

//编号

Composite codeComposite = new Composite(this, SWT.None);

codeComposite.setLayout(new FillLayout());

Label codeLabel = new Label(codeComposite, SWT.RIGHT);

codeLabel.setText("编号:");

codeText = new Text(codeComposite, SWT.SINGLE|SWT.BORDER);

//姓名

Composite nameComposite = new Composite(this, SWT.None);

nameComposite.setLayout(new FillLayout());

Label nameLabel = new Label(nameComposite, SWT.RIGHT);

nameLabel.setText("姓名:");

nameText = new Text(nameComposite, SWT.SINGLE|SWT.BORDER);

//性别

Composite genderComposite = new Composite(this, SWT.None);

genderComposite.setLayout(new FillLayout());

Label genderLabel = new Label(genderComposite, SWT.RIGHT);

genderLabel.setText("性别:");

genderCombo = new Combo(genderComposite, SWT.READ_ONLY);

genderCombo.add("");

genderCombo.add("男");

genderCombo.add("女");

//省份证

Composite idNumberComposite = new Composite(this, SWT.None);

idNumberComposite.setLayout(new FillLayout());

Label idNumberLabel = new Label(idNumberComposite, SWT.RIGHT);

idNumberLabel.setText("省份证号:");

idNumberText = new Text(idNumberComposite, SWT.SINGLE|SWT.BORDER);

//备注

Composite remarkComposite = new Composite(this, SWT.None);

remarkComposite.setLayout(new FillLayout());

Label remarkLabel = new Label(remarkComposite, SWT.RIGHT);

remarkLabel.setText("备注:");

remarkText = new Text(remarkComposite, SWT.SINGLE|SWT.BORDER);

//生日 Composite birthdayComposite = new Composite(this, SWT.None); birthdayComposite.setLayout(new FillLayout()); Label birthdayLabel = new Label(birthdayComposite, SWT.RIGHT); birthdayLabel.setText("生日:"); birthdayDateTime = new DateTime(birthdayComposite, SWT.DATE|SWT.BORDER);

JasonRight 2011-03-15

你的图是qq空间里面的,别人看不到。修复一下。

根据代码,问题是DateTime的宽度和其他的Text控件不一样。所以默认当然不能对齐。如果更改某个Text里面的内容,会发现Text也不对齐了。

remarkText.setText("111111111111");



可以用FormLayout来精确定位, 但DateTime的doc说到,

it does not make sense to add children to it, or set a layout on it.



所以必须把DateTime封装在另一个Composite里,并把Text改成左对齐。

Label genderLabel = new Label(genderComposite, SWT.LEFT);

 

		//生日 
		Composite birthdayComposite = new Composite(this, SWT.NONE); 
		birthdayComposite.setLayout(new FormLayout()); 
		Label birthdayLabel = new Label(birthdayComposite, SWT.LEFT);
		birthdayLabel.setText("生日:");

		Composite dateTimeComp = new Composite(birthdayComposite, SWT.NONE);
		dateTimeComp.setLayout(new FillLayout());
		DateTime birthdayDateTime = new DateTime(dateTimeComp, SWT.DATE|SWT.BORDER);
		
		FormData lableFormData = new FormData();
		lableFormData.left = new FormAttachment(0);
		lableFormData.top = new FormAttachment(0);
		lableFormData.bottom = new FormAttachment(100);
		
		birthdayLabel.setLayoutData(lableFormData);
		
		FormData dateFormData = new FormData();
		final int offset = 12;//can be adjusted
		dateFormData.left = new FormAttachment(birthdayLabel, offset);
		dateFormData.right = new FormAttachment(100);
		dateFormData.top = new FormAttachment(0);
		dateFormData.bottom = new FormAttachment(100);
		dateTimeComp.setLayoutData(dateFormData);
liuzhiyuan 2011-03-16
我不太喜欢FormData,感觉挺麻烦,而且有点硬编码的感觉。
我把birthdayComposite的FillLayout改成GridLayout,虽然对不齐,但比以前好多了,先凑活用吧。不知道RowLayout能不能解决问题。
不管怎么说谢谢JasonRight。
Global site tag (gtag.js) - Google Analytics