SWT综合布局示例

本文详细介绍了如何在SWT中使用各种布局管理器,包括Shell、Button等组件的布置,以及导入和网格布局的实用技巧。通过实例代码解析,帮助读者掌握SWT界面设计的核心概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

package edu.ch4;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.SWT;
public class Sample4_17 {
public static void main(String[] args) {
Display display 
= new Display();
Shell shell 
= new Shell(display);
shell.setText(
"FormLayout示例");
FormLayout formlayout 
= new FormLayout(); //创建表格布局对象
shell.setLayout(formlayout);
Label label
=new Label(shell,SWT.BORDER); //在shell中创建Label对象
label.setText("Label One");
FormData data 
= new FormData();
data.top 
= new FormAttachment(05); //Label与shell上边框相距5象素
data.left = new FormAttachment(05); // Label与shell左边框相距5象素
data.bottom = new FormAttachment(50-5);//Label在shell水平中线上方5象素
data.right = new FormAttachment(50-5); // Label在shell垂直中线左边5象素
label.setLayoutData(data);
Composite composite 
= new Composite(shell, SWT.NONE);//创建面板
GridLayout gridLayout = new GridLayout(); //创建网格布局对象
gridLayout.marginHeight = 0;
gridLayout.marginWidth 
= 0;
composite.setLayout(gridLayout); 
//设置面板布局方式为网格布局
Button b1 = new Button(composite, SWT.PUSH); //在composite上创建button B1
b1.setText("B1");
GridData gridData 
= new GridData(GridData.FILL_BOTH); //设置布局方式为双向填充
b1.setLayoutData(gridData);
Button b2 
= new Button(composite, SWT.PUSH); //B2设置同B1
b2.setText("B2");
gridData 
= new GridData(GridData.FILL_BOTH);
b2.setLayoutData(gridData);
Button b3 
= new Button(composite, SWT.PUSH); //B2设置同B1
b3.setText("B3");
gridData 
= new GridData(GridData.FILL_BOTH);
PDF 文件使用 
"pdfFactory" 试用版本创建 www.fineprint.cn
b3.setLayoutData(gridData);
data 
= new FormData(); //创建FormData对象
data.top = new FormAttachment(05); //设置composite距shell上边框5象素
data.left = new FormAttachment(label, 5);//设置composite距label 5象素
data.bottom = new FormAttachment(50-5);//设置composite在shell水平中线上方5象素
data.right = new FormAttachment(100-5);//设置composite在shell右边框的左侧5象素
composite.setLayoutData(data); //设置composite的布局数据
Text text=new Text(shell,SWT.BORDER); //创建Text对象
text.setText("Text");
data 
= new FormData(); //创建表格布局数据
data.top = new FormAttachment(label, 5); //text上方离label 5象素
data.left = new FormAttachment(05); // text左边离shell左边框5象素
data.bottom = new FormAttachment(100-5); // text下边框离shell下边框5象素
data.right = new FormAttachment(100-5); // text右边框离shell右边框5象素
text.setLayoutData(data); //设置text的布局数据
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}

}

display.dispose();
}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值