首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 开源 FAQ 第二书店 博文视点 程序员
频道: 研发 数据库 中间件 信息化 视频 .NET Java 游戏 移动 服务: 人才 外包 培训
    图书品种:235680
       
热门搜索: ASP.NET Ajax Spring Hibernate Java

13.6  容 器 组 件

容器组件能够作为其他组件的父组件,包含其他的组件。

13.6.1  面板容器Composite

Composite是SWT中常用的组件,它的作用相当于一个容器面板,用户可以创建一个面板对象,通过面板对象来添加其他的子组件。

在GUI的开发中,为了布局的方便,常常用Composite来添加子组件。在Composite上设置相应的布局,从而使局部的布局效果不一样。一般来说,面板可以设置成SWT.NONE的样式,另外还可以给Composite设置成带边框的样式。创建一个面板很简单,代码如例程13-8所示。

例程13-8  ComposizeExample.java

public class ComposizeExample {

     public static void main(String[] args) {

         Display display = new Display();

         Shell shell = new Shell(display);

         shell.setLayout(new FillLayout());

        

         //创建面板

         Composite composite1 = new Composite(shell, SWT.BORDER);

         composite1.setLayout(new RowLayout(SWT.VERTICAL));

         new Button(composite1, SWT.RADIO).setText("John");

         new Button(composite1, SWT.RADIO).setText("Paul");

         new Button(composite1, SWT.RADIO).setText("George");

        new Button(composite1, SWT.RADIO).setText("Ringo");

        

        Composite composite2 = new Composite(shell, SWT.NONE);

        composite2.setLayout(new RowLayout(SWT.VERTICAL));

         new Button(composite2, SWT.CHECK).setText("Barry");

         new Button(composite2, SWT.CHECK).setText("Robin");

         new Button(composite2, SWT.CHECK).setText("Maurice");

        

         shell.setSize(200, 200);

         shell.open();

         while (!shell.isDisposed()) {

           if (!display.readAndDispatch()) {

             display.sleep();

           }

         }

         display.dispose();

     }

}

以上程序创建了两个面板,一个设置成SWT.BORDER样式(带边框),另一个设置成SWT.NONE样式(未添加样式),程序运行效果如图13-15所示。

图13-15  Composite组件

Composite组件是一个面板容器,Composite组件还能够包含非容器,也能包含其他容器组件,包括Composite组件。

13.6.2  分组容器Group

在SWT中,Group组件代表分组框,用户可以通过分组框把内容上相关的子组件组合在一起,例如某一类人、某一种水果等。

用户可以通过Group建立一个特定的分组,每一个分组有一个名称,可以通过Group的setText设置分组的显示名称,代码如例程13-9所示。

例程13-9  GroupExample.java

public class GroupExample {

       public static void main(String[] args) {

         Display display = new Display();

         Shell shell = new Shell(display);

         shell.setLayout(new FillLayout());

         //添加分组框

         Group group1 = new Group(shell, SWT.SHADOW_IN);

         //设置分组框的显示标题

         group1.setText("Who's your favorite?");

         group1.setLayout(new RowLayout(SWT.VERTICAL));

         new Button(group1, SWT.RADIO).setText("John");

         new Button(group1, SWT.RADIO).setText("Paul");

         new Button(group1, SWT.RADIO).setText("George");

        new Button(group1, SWT.RADIO).setText("Ringo");

        

         Group group2 = new Group(shell, SWT.NO_RADIO_GROUP);

         group2.setText("Who's your favorite?");

         group2.setLayout(new RowLayout(SWT.VERTICAL));

         new Button(group2, SWT.CHECK).setText("Barry");

         new Button(group2, SWT.CHECK).setText("Robin");

         new Button(group2, SWT.CHECK).setText("Maurice");

        

         shell.setSize(300, 200);

         shell.open();

         while (!shell.isDisposed()) {

           if (!display.readAndDispatch()) {

             display.sleep();

           }

         }

         display.dispose();

       }

     }

以上程序创建了两个分组框,每个分组框都设定了相关显示标题,程序运行效果如图13-16所示。

图13-16  Group组件

Group组件和Composite组件的不同之处在于Group组件可以设置标题,用来表示分组的信息。

13.6.3  分页签容器TabFolder和TabItem

在很多应用程序中,由于信息量比较大,一页可能显示不下所有的信息,这就要求多页组件的支持。

在SWT中,通过TabFolder组织分页框,TabItem为每一个分页,创建一个分页框,步骤如下:

(1)在容器中创建一个TabFolder对象,例如“TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);”。

(2)在TabFolder中创建一个TabItem对象,例如“TabItem tabItem = new TabItem  (tabFolder, SWT.NULL);”。

(3)设置TabItem的显示标签,例如“tabItem.setText("Very good");”。

(4)设置每一分页的Control对象(即在此页的顶层组件,一般是容器),例如“tabItem.setControl(composite);”。

为了更好地掌握分页框,下面通过一个实例演示如何创建分页框,代码如例程13-10所示。

例程13-10  TabFolderExample.java

public class TabFolderExample {

       public static void main(String[] args) {

         Display display = new Display();

         final Shell shell = new Shell(display);

         shell.setText("Tab Folder Example");

         shell.setSize(450, 250);

         //创建分页框

         final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);

         for (int loopIndex = 0; loopIndex < 3; loopIndex++) {

           //创建分页项

           TabItem tabItem = new TabItem(tabFolder, SWT.NULL);

           //设置分页项的标题

           tabItem.setText("Tab " + loopIndex);

           Text text = new Text(tabFolder, SWT.BORDER);

           text.setText("This is page " + loopIndex);

           //设置Control对象

           tabItem.setControl(text);

         }

        

         TabItem tabItem = new TabItem(tabFolder, SWT.NULL);

         tabItem.setText("Tab " + 3);

         Composite composite = new Composite(tabFolder, SWT.BORDER);

         Text text = new Text(composite, SWT.BORDER);

         text.setText("This is page " + 3);

         text.setBounds(10, 10, 100, 20);

         //设置Control对象

         tabItem.setControl(composite);

        

        

         tabFolder.setSize(400, 200);

         shell.open();

         while (!shell.isDisposed()) {

           if (!display.readAndDispatch())

             display.sleep();

         }

         display.dispose();

       }

     }

以上程序创建了4个分页,其中第4个分页的Control对象为一个容器,程序运行效果如图13-17所示。

图13-17  TabFolder\TabItem组件

通常,每一个分页项都是一个容器组件,这样每一个分页就是一个容器,可以包含其他的组件。

13.6.4  分隔框容器SashForm

SashForm是SWT中的分隔框组件,在容器中,有时需要调整容器中元素的大小,这就要用到SashForm。在SWT中添加分隔框的步骤如下:

(1)在容器中创建SashForm对象,例如“sashForm = new SashForm(shell, SWT. HORIZONTAL);”。

(2)在SashForm对象中添加分隔项(即分隔框的子组件),可以是SashForm或Control的子类,例如“Text text1 = new Text(sashForm, SWT.CENTER);”。

(3)设置SashForm中分隔项显示比例的权重(可选),例如“sashForm.setWeights(new int[]{1, 2, 3});”,表示3个分隔项之间的显示比例为“1:2:3”。

为了更好地掌握分隔框,下面通过一个实例演示如何创建分隔框,代码如例程13-11所示。

例程13-11  SashFormExample.java

public class SashFormExample {

       Display display = new Display();

       Shell shell = new Shell(display);

       SashForm sashForm;

       SashForm sashForm2;

       public SashFormExample() {

         shell.setLayout(new FillLayout());

         //添加分隔框

         sashForm = new SashForm(shell, SWT.HORIZONTAL);

         //添加分隔项

         Text text1 = new Text(sashForm, SWT.CENTER);

         text1.setText("Text in pane #1");

         Text text2 = new Text(sashForm, SWT.CENTER);

         text2.setText("Text in pane #2");

        

         //添加子分隔框

         sashForm2 = new SashForm(sashForm, SWT.VERTICAL);

         final Label labelA = new Label(sashForm2, SWT.BORDER | SWT.CENTER);

         labelA.setText("Label in pane A");

         final Label labelB = new Label(sashForm2, SWT.BORDER |SWT.CENTER);

         labelB.setText("Label in pane B");

         //添加ControlListener监听器

         text1.addControlListener(new ControlListener() {

           public void controlMoved(ControlEvent e) {

           }

           public void controlResized(ControlEvent e) {

             System.out.println("Resized");

           }

         });

         //设置分隔框中组件显示比例的权重

         sashForm.setWeights(new int[]{1, 2, 3});

         labelA.addMouseListener(new MouseListener() {

           public void mouseDoubleClick(MouseEvent e) {

             if(sashForm2.getMaximizedControl() == labelA)

               sashForm2.setMaximizedControl(null);

             else

               sashForm2.setMaximizedControl(labelA);

           }

           public void mouseDown(MouseEvent e) {

           }

           public void mouseUp(MouseEvent e) {

           }

         });

         shell.setSize(450, 200);

         shell.open();

         while (!shell.isDisposed()) {

           if (!display.readAndDispatch()) {

             // If no more entries in event queue

             display.sleep();

           }

         }

         display.dispose();

       }

       public static void main(String[] args) {

         new SashFormExample();

       }

     }

以上程序中为嵌套的分隔框组件,第一层分隔项的显示比例为“1:2:3”,程序运行效果如图13-18所示。

图13-18  SashForm组件

SashForm和标签分隔符不同,SashForm分隔的几个组件大小可以通过SashForm调整。

查看所有评论(0)条】

最近评论



正在载入评论列表...
热点评论