跟我学之用FormLayout打造自适应窗体大小的控件布局
来源:互联网 发布:sql2008创建数据库 编辑:程序博客网 时间:2024/05/19 15:42
我胖
我瘦
我标准
以上就是一个窗体在不同大小下的效果,控件随着窗体的大小改变大小和位置,这就是FormLayout的布局方式的的功劳。
FormLayout的布局方式的精髓就两个字“相对”。
下面先看看设计界面:
在设计器里面点击文本框[name_text]可以看到上下左右各出现一个小箭头,这四个箭头就是用来设置该控件在四个方向上的相对位置。
先看看上:
点击上箭头会出现五个小按钮,
第一个是设置控件[上边]相对容器[上边],偏移量为具体数字。
第二个是置控件[上边]相对容器[底边],偏移量为具体数字。
第三个是设置控件[上边]相对容器[上边],偏移量为百分比。
第四个是设置控件[上边]相对容器内某控件的位置,偏移量为具体数字。
第五个是取消设置。
左箭头、右箭头和下箭头点击以后与此类似。
举例说明:
对于文本框[name_text],我们这样设置:
[上边]相对容器[上边],偏移量为具体数字。
[左边]相对容器[左边],偏移量为具体数字。
[右边]相对容器[右边],偏移量为具体数字。(当容器改变宽度的时候,控件宽度跟着改变)
[下边]相对容器[上边],偏移量为具体数字。(当容器高度变化时,高度不变)
对于文本框[name_remark],我们这样设置:
[上边]相对容器[上边],偏移量为具体数字。
[左边]相对容器[左边],偏移量为具体数字。
[右边]相对容器[右边],偏移量为具体数字。(当容器改变宽度的时候,控件宽度跟着改变)
[下边]相对容器[下边],偏移量为具体数字。(当容器高度变化时,控件高度跟着改变)
通过A相对B这种方式。我们能制作出非常实用美观的界面。
以下是代码:
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("说明");
//
}
}
附:
表一:FormAttachment数据成员
Attribute
Description
int alignment
指定是以另外一个部件的哪一边为基准的.可能的值是:SWT.TOP, SWT.CENTER, 和 SWT.BOTTOM.默认是以最近的一边为基准.
Control control
指定FormAttachment是以哪个部件为参照物.
int denominator
指定分母.默认值为100
int numerator
指定分子
int offset
指定离composite边界或部件的边界的偏移量.单位是象素.
FormAttachment 拥有5个构造函数,没有一个是空的,它们如下表(表二)所示:
表二:FormAttachment的构造函数:
Constructor
Description
FormAttachment(Control control)
以另外的某个部件为参照物.
FormAttachment(Control control, int offset)
以另外的某个部件为参照物,加上偏移量.
FormAttachment(Control control, int offset, int alignment)
以另外的某个部件为参照物,加上偏移量和alignment
FormAttachment(int numerator)
设定分子.分母为100,没有偏移量
FormAttachment(int numerator, int offset)
指定分子和偏移量和100的分母
FormAttachment(int numerator, int denominator, int offset)
特定的分子,分母,偏移量
FormData最多包含4个FormAttachment实例,每个对应了与它联系的部件的一边.另外,FormData也可以指定宽和高.表四列出了FormData的数据成员:
Attribute
Description
FormAttachment bottom
The FormAttachment corresponding to the bottom side of the control.
这个FormAttachment用来指定部件的底部位置
int height
这个部件的高度.单位为象素.
FormAttachment left
这个FormAttachment用来指定部件的左部位置
FormAttachment right
这个FormAttachment用来指定部件的右部位置
FormAttachment top
这个FormAttachment用来指定部件的顶部位置
int width
这个部件的宽度.单位为象素.
- 跟我学之用FormLayout打造自适应窗体大小的控件布局
- 用FormLayout打造自适应窗体大小的控件布局
- Qt Creator 窗体控件自适应窗口大小布局
- Qt Creator 窗体控件自适应窗口大小布局
- Qt Creator 窗体控件自适应窗口大小布局
- Qt Creator 窗体控件自适应窗口大小布局
- Qt Creator 窗体控件自适应窗口大小布局
- Qt Creator 窗体控件自适应窗口大小布局
- Qt Creator 窗体控件自适应窗口大小布局
- Qt Creator 窗体控件自适应窗口大小布局
- Qt Creator 窗体控件自适应窗口大小布局
- Qt Creator 窗体控件自适应窗口大小布局
- Qt Creator 窗体控件自适应窗口大小布局
- Qt Creator 窗体控件自适应窗口大小布局
- Qt Creator 窗体控件自适应窗口大小布局
- Qt Creator 窗体控件自适应窗口大小布局
- Qt Creator 窗体控件自适应窗口大小布局
- Qt Creator 窗体控件自适应窗口大小布局
- 程序员每天该做的事
- BEA SOA解决之道
- Appeon for PowerBuilder 5.0 的新特性
- 澄清对基金认知上的几点误区
- lwIP TCP/IP协议栈的设计与实现
- 跟我学之用FormLayout打造自适应窗体大小的控件布局
- CSDN大侠们的太极修为
- 用Live Writer来写Blog
- 用AOP实现观察者模式
- Singleton的Application Context
- 关闭整个应用程序的内核模式缓存
- 解决方案大全
- MDA核心之MOF原理和实现
- 程序设计技巧:设计模式之Factory(工厂模式)