SWT/JFace常用组件----容器类

通常,组件构建在容器类中,容器构建在主窗体(shell)中,主窗体也是容器,也就是说,容器不仅可以容纳组件,也可以容纳容器。有了容器,就可以通过 它来对组件进行集体操作。例如,容器在界面上移动时,其上的组件也会随着容器移动,容器隐藏,其组件也会被隐藏,容器销毁(dispose),其组件也会 被销毁。

1 面板
面板(Composite类)是最常用的容器。主窗体(shell)是面板(Composite)的子类。面板的构造方法格式如下:
Composite(Composite parent,int style)
第 一个参数表示该容器创建在哪个容器上,第二个参数表示容器的式样。Composite的式样一般都是用SWT.NONE,这时Composite在界面是 不显示出来的,只是发挥着容器的作用。如果要让容器形成凹陷效果,可以用SWT.BORDER式样。例如,在主窗体中创建一个容器:
Composite composite=new Composite(shell,SWT.NONE);
Composite的常用方法:
getLayout():得到布局管理器。
getLayoutData():得到布局数据。
getParent():得到容纳该容器的父容器。
getShell():得到容纳该容器的Shell。
layout():将容器上的组件重新布局,相当于刷新。


package edu.ch4;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
public class Sample4_8 {
public static void main(String[] args) {
Display display=new Display();//创建一个display对象。
final Shell shell=new Shell(display);//shell是程序的主窗体
shell.setText("容器示例");
Composite composite1=new Composite(shell,SWT.NONE);
composite1.setBounds(10,10,100,50);
Composite composite2=new Composite(shell,SWT.BORDER);
composite2.setBounds(120,10,100,50);
Label lb1=new Label(composite1,SWT.NONE);
lb1.setText("面板1");
lb1.pack();
Label lb2=new Label(composite2,SWT.NONE);
lb2.setText("面板2");
lb2.pack();
shell.pack();
shell.open();
while(!shell.isDisposed()){ //如果主窗体没有关闭则一直循环
if(!display.readAndDispatch()){ //如果display不忙
display.sleep(); //休眠
}
}
display.dispose(); //销毁display
}
}



2 分组框
分组框(Group类)是面板(Composite类)的子类,所以两者用法基本相同。主要区别是Group显示有一个方框,且方框线上还可以显示说明文字。

package edu.ch4;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
public class Sample4_9 {
public static void main(String[] args) {
Display display=new Display();//创建一个display对象。
final Shell shell=new Shell(display);//shell是程序的主窗体
shell.setText("分组框示例");
Group group1=new Group(shell,SWT.NONE); //创建分组框
group1.setText("录入信息"); //设置分组框说明信息
group1.setBounds(10,20,200,100);
Label lb1=new Label(group1,SWT.NONE); //在分组框中加入组件
lb1.setText("姓名:");
lb1.setBounds(10,20,70,20);
Text text1=new Text(group1,SWT.BORDER);
text1.setBounds(90,20,70,20);
Label lb2=new Label(group1,SWT.NONE);
lb2.setText("地址:");
lb2.setBounds(10,50,70,20);
Text text2=new Text(group1,SWT.BORDER);
text2.setBounds(90,50,70,20);
shell.pack();
shell.open();
while(!shell.isDisposed()){ //如果主窗体没有关闭则一直循环
if(!display.readAndDispatch()){ //如果display不忙
display.sleep(); //休眠
}
}
display.dispose(); //销毁display
}
}


3 选项卡
选项卡包括一个选项卡(TabFolder类)和一个选项页(TabItem类),TabFolder是容器,可以容纳其他容器和组件,但TabItem 不是容器,可以把它看成是一个选项标签,TabFolder通过TabItem来对其中的组件进行控制。每一个TabItem用setControl() 方法来控制一个界面组件。

package edu.ch4;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
public class Sample4_10 {
public static void main(String[] args) {
Display display=new Display();//创建一个display对象。
final Shell shell=new Shell(display);//shell是程序的主窗体
shell.setText("选项卡示例");
TabFolder tabFolder=new TabFolder(shell,SWT.NONE);//声明一个选项卡容器
tabFolder.setBounds(5,5,180,130); //设置选项卡的位置和大小
TabItem tabItem1=new TabItem(tabFolder,SWT.NONE);//声明第1个选项页
tabItem1.setText("选项1"); //设置选项页的标题
{
//创建第1个分组框,建立在tabFolder上
Group group1=new Group(tabFolder,SWT.NONE);
group1.setText("录入信息"); //设置分组框说明信息
tabItem1.setControl(group1); //让tabItem1控制group1
Label lb1=new Label(group1,SWT.NONE); //注意Label建立在group1上
lb1.setText("姓名:");
lb1.setBounds(10,20,70,20);
Text text1=new Text(group1,SWT.BORDER);
text1.setBounds(90,20,70,20);
Label lb2=new Label(group1,SWT.NONE);
lb2.setText("地址:");
lb2.setBounds(10,50,70,20);
Text text2=new Text(group1,SWT.BORDER);
text2.setBounds(90,50,70,20);
}
TabItem tabItem2=new TabItem(tabFolder,SWT.NONE); //声明第2个选项页
tabItem2.setText("选项2");
{
//创建第2个分组框,建立在tabFolder上
Group group2=new Group(tabFolder,SWT.NONE);
tabItem2.setControl(group2); //让tabItem2控制group2
group2.setText("兴趣爱好");
Button bt1=new Button(group2,SWT.CHECK);
bt1.setBounds(20,20,70,20);
bt1.setText("音乐");
Button bt2=new Button(group2,SWT.CHECK);
bt2.setBounds(20,50,70,20);
bt2.setText("美术");
Button bt3=new Button(group2,SWT.CHECK);
bt3.setBounds(20,80,70,20);
bt3.setText("体育");
}
shell.pack();
shell.open();
while(!shell.isDisposed()){ //如果主窗体没有关闭则一直循环
if(!display.readAndDispatch()){ //如果display不忙
display.sleep(); //休眠
}
}
display.dispose(); //销毁display
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值