用FormLayout打造自适应窗体大小的控件布局

作SWT程序界面时很多窗体的大小是可变。在大小不固定的窗体里怎么使里面的控件在任意大小下都能保持美观是将让人头疼的事情。FormLayout的布局方式就是解决这一问题的灵丹妙药。上图是王道,看图先:
我胖


我瘦



我标准



以上就是一个窗体在不同大小下的效果,控件随着窗体的大小改变大小和位置,这就是FormLayout的布局方式的的功劳。
FormLayout的布局方式的精髓就两个字“相对”。

下面先看看设计界面:




在设计器里面点击文本框[name_text]可以看到上下左右各出现一个小箭头,这四个箭头就是用来设置该控件在四个方向上的相对位置。
先看看上:
点击上箭头会出现五个小按钮
第一个是设置控件[上边]相对容器[上边],偏移量为具体数字。
第二个是置控件[上边]相对容器[底边],偏移量为具体数字。
第三个是设置控件[上边]相对容器[上边],偏移量为百分比。
第四个是设置控件[上边]相对容器内某控件的位置,偏移量为具体数字。
第五个是取消设置。

左箭头、右箭头和下箭头点击以后与此类似。

举例说明:
对于文本框[name_text],我们这样设置:
[上边]相对容器[上边],偏移量为具体数字。
[左边]相对容器[左边],偏移量为具体数字。
[右边]相对容器[右边],偏移量为具体数字。(当容器改变宽度的时候,控件宽度跟着改变)
[下边]相对容器[上边],偏移量为具体数字。(当容器高度变化时,高度不变)

对于文本框[name_remark],我们这样设置:
[上边]相对容器[上边],偏移量为具体数字。
[左边]相对容器[左边],偏移量为具体数字。
[右边]相对容器[右边],偏移量为具体数字。(当容器改变宽度的时候,控件宽度跟着改变)
[下边]相对容器[下边],偏移量为具体数字。(当容器高度变化时,控件高度跟着改变)

通过A相对B这种方式。我们能制作出非常实用美观的界面。
以下是代码:

import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class TestFormLayout { private Text text_remark; private Text text_name; protected Shell shell; /** * Launch the application * @param args */ public static void main(String[] args) { try { TestFormLayout window = new TestFormLayout(); window.open(); } catch (Exception e) { e.printStackTrace(); } } /** * Open the window */ public void open() { final Display display = Display.getDefault(); createContents(); shell.open(); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } /** * Create contents of the window */ protected void createContents() { shell = new Shell(); shell.setLayout(new FillLayout()); shell.setSize(500, 375); shell.setText("SWT Application"); Label label_remark; final Composite composite = new Composite(shell, SWT.NONE); composite.setLayout(new FormLayout()); final Label label_name = new Label(composite, SWT.NONE); final FormData formData = new FormData(); formData.top = new FormAttachment(0, 25); formData.left = new FormAttachment(0, 30); formData.right = new FormAttachment(0, 60); label_name.setLayoutData(formData); label_name.setText("姓名"); text_name = new Text(composite, SWT.BORDER); formData.bottom = new FormAttachment(text_name, 0, SWT.BOTTOM); final FormData formData_1 = new FormData(); formData_1.top = new FormAttachment(0, 25); formData_1.right = new FormAttachment(100, -32); formData_1.bottom = new FormAttachment(0, 43); formData_1.left = new FormAttachment(label_name, 5, SWT.DEFAULT); text_name.setLayoutData(formData_1); text_remark = new Text(composite, SWT.BORDER); final FormData formData_2 = new FormData(); formData_2.bottom = new FormAttachment(100, -16); formData_2.right = new FormAttachment(100, -32); formData_2.top = new FormAttachment(0, 62); formData_2.left = new FormAttachment(0, 65); text_remark.setLayoutData(formData_2); label_remark = new Label(composite, SWT.NONE); final FormData formData_3 = new FormData(); formData_3.top = new FormAttachment(44, 0); formData_3.bottom = new FormAttachment(51, 0); formData_3.right = new FormAttachment(0, 60); formData_3.left = new FormAttachment(0, 30); label_remark.setLayoutData(formData_3); label_remark.setText("说明");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值