ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 容器组件(七)

容器组件(七)

原创 Linux操作系统 作者:阿-甘 时间:2008-06-02 15:20:15 0 删除 编辑
容器组件能够作为其它组件的父组件,包含其它的组件。包括面板容器Composite、分组容器Group、分页签容器TabFolder和TabItem和分隔框容器SashForm等。

面板容器Composite

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

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

例程1  ComposizeExample.java

 

/**

 * 为了节省篇幅,所有的import类已经被注释

 * 读者可以通过ctrl+shift+o快捷键,自动引入所依赖的类

 * 如果有问题可发邮件到ganshm@gmail.com

 * */

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样式(未添加样式),程序运行效果如图1所示。

图1  Composite组件

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

分组容器Group

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

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

例程2  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();

           }

     }

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

图2  Group组件

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

分页签容器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);”。

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

例程3  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对象为一个容器,程序运行效果如图3所示。

图3  TabFolder\TabItem组件

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

分隔框容器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”。

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

例程4  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”,程序运行效果如图4所示。

图4  SashForm组件

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

image002.jpg

image004.jpg

image006.jpg

image008.jpg

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/13081368/viewspace-329657/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-02-14

  • 博文量
    49
  • 访问量
    246601