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
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM