【原创】展现组件(八)

SWT中还有一些常用的组件,它们可以使组件更有表现力,且称之为展现组件,它们在SWT开发中也是不可缺少的,包括菜单组件、工具栏组件ToolBar和ToolItem、工具栏组件CoolBar和CoolItem、滚动组件Slider、刻度组件Scale和进度条组件ProgressBar等。

菜单组件

在程序中,菜单是提供信息比较好的方式,SWT中通过Menu和MenuItem实现菜单和菜单项,在程序中添加菜单的步骤如下。

(1)创建Menu对象,并指定创建的样式,例如“menuBar = new Menu(shell,  SWT.BAR);”。

(2)创建MenuItem对象,并指定创建样式,例如“fileEnglishItem = new MenuItem (fileMenu, SWT.RADIO);”。

(3)设置Menu和MenuItem的关联(Menu中还可以有子Menu),例如“fileMenuHeader. setMenu(fileMenu);”。

(4)添加MenuItem的事件监听器,例如“fileEnglishItem.addSelectionListener(new RadioItemListener());”。

为了更好地掌握菜单,下面通过一个实例演示如何创建菜单,代码如例程1所示。

例程1  MenuExample.java

/**

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

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

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

 * */

public class MenuExample {

 

           Display display;

 

           Shell shell;

 

           Menu menuBar, fileMenu, editMenu;

 

           MenuItem fileMenuHeader, editMenuHeader;

 

           MenuItem fileExitItem, fileSaveItem, fileEnglishItem, fileGermanItem,

               editCopyItem;

 

           Text text;

 

           public MenuExample() {

             display = new Display();

             shell = new Shell(display);

             shell.setText("Menu Example");

             shell.setSize(300, 200);

 

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

             text.setBounds(80, 50, 150, 25);

 

             //添加主菜单项

             menuBar = new Menu(shell, SWT.BAR);

             //添加一级子菜单

             fileMenuHeader = new MenuItem(menuBar, SWT.CASCADE);

             fileMenuHeader.setText("&File");

 

        //添加一级子菜单的菜单项

             fileMenu = new Menu(shell, SWT.DROP_DOWN);

             fileMenuHeader.setMenu(fileMenu);

 

             fileSaveItem = new MenuItem(fileMenu, SWT.PUSH);

             fileSaveItem.setText("&Save");

 

             fileEnglishItem = new MenuItem(fileMenu, SWT.RADIO);

             fileEnglishItem.setText("English");

 

             fileGermanItem = new MenuItem(fileMenu, SWT.RADIO);

             fileGermanItem.setText("German");

 

             fileExitItem = new MenuItem(fileMenu, SWT.PUSH);

             fileExitItem.setText("E&xit");

 

             editMenuHeader = new MenuItem(menuBar, SWT.CASCADE);

             editMenuHeader.setText("&Edit");

 

             editMenu = new Menu(shell, SWT.DROP_DOWN);

             editMenuHeader.setMenu(editMenu);

 

             editCopyItem = new MenuItem(editMenu, SWT.PUSH);

             editCopyItem.setText("&Copy");

 

             //添加菜单项的事件监听器

             fileExitItem.addSelectionListener(new MenuItemListener());

             fileSaveItem.addSelectionListener(new MenuItemListener());

             editCopyItem.addSelectionListener(new MenuItemListener());

 

             fileEnglishItem.addSelectionListener(new RadioItemListener());

             fileGermanItem.addSelectionListener(new RadioItemListener());

 

             shell.setMenuBar(menuBar);

             shell.open();

             while (!shell.isDisposed()) {

               if (!display.readAndDispatch())

                 display.sleep();

             }

             display.dispose();

           }

 

           class MenuItemListener extends SelectionAdapter {

             public void widgetSelected(SelectionEvent event) {

                            text.setText("You selected " + ((MenuItem) event.widget).getText());

                            if (((MenuItem) event.widget).getText().equals("E&xit")) {

                                     shell.close();

                            }

                   }

           }

 

           class RadioItemListener extends SelectionAdapter {

             public void widgetSelected(SelectionEvent event) {

               MenuItem item = (MenuItem) event.widget;

               text.setText(item.getText() + " is on.");

             }

           }

 

           public static void main(String[] args) {

             MenuExample menuExample = new MenuExample();

           }

     }

以上程序中添加了主菜单,并在主菜单中添加了两个子菜单项,子菜单项添加了相应的事件响应机制,程序运行效果如图1所示。

13081368_200806110859241.jpg

图1  Menu\MenuItem组件

菜单是可以级联的,在子菜单中还能够包含其它的菜单项。

工具栏组件ToolBar和ToolItem

ToolBar是SWT中的工具栏组件,ToolItem是工具栏中的工具项(一般表现为按钮或分隔符,也可以是其他组件),在程序中添加工具栏的步骤如下:

1.  创建ToolBar对象,并指定创建的样式,例如“toolBar = new ToolBar(shell, SWT.FLAT | SWT.WRAP | SWT.RIGHT);”。

2.  创建ToolItem对象,并指定创建样式,例如“ToolItem itemPush = new ToolItem  (toolBar, SWT.PUSH);”。

3.  设置ToolItem的图标和相关属性,例如“itemPush.setImage(icon);”。

4. 添加ToolItem的事件监听器,例如“itemPush.addListener(SWT.Selection,selectionListener);”。

为了更好地掌握工具栏组件,下面通过一个实例演示如何创建工具栏组件,代码如例程2所示。

例程2  ToolBarExample.java

public class ToolBarExample {

         Display display = new Display();

 

         Shell shell = new Shell(display);

 

         ToolBar toolBar;

 

         public ToolBarExample() {

                   //添加工具栏

                   toolBar = new ToolBar(shell, SWT.FLAT | SWT.WRAP | SWT.RIGHT);

                   //添加工具项

                   ToolItem itemPush = new ToolItem(toolBar, SWT.PUSH);

                   itemPush.setText("PUSH item");

                   //设置工具项的显示图标

                   //Image icon = new Image(shell.getDisplay(), "icons/new.gif");

                   //itemPush.setImage(icon);

                   ToolItem itemCheck = new ToolItem(toolBar, SWT.CHECK);

                   itemCheck.setText("CHECK item");

                   ToolItem itemRadio1 = new ToolItem(toolBar, SWT.RADIO);

                   itemRadio1.setText("RADIO item 1");

                   ToolItem itemRadio2 = new ToolItem(toolBar, SWT.RADIO);

                  itemRadio2.setText("RADIO item 2");

                   ToolItem itemSeparator = new ToolItem(toolBar, SWT.SEPARATOR);

 

                   final ToolItem itemDropDown = new ToolItem(toolBar, SWT.DROP_DOWN);

                   itemDropDown.setText("DROP_DOWN item");

                   itemDropDown.setToolTipText("Click here to see a drop down menu ...");

                   final Menu menu = new Menu(shell, SWT.POP_UP);

                   new MenuItem(menu, SWT.PUSH).setText("Menu item 1");

                   new MenuItem(menu, SWT.PUSH).setText("Menu item 2");

                   new MenuItem(menu, SWT.SEPARATOR);

                   new MenuItem(menu, SWT.PUSH).setText("Menu item 3");

                   //设置工具项的事件监听器

                   itemDropDown.addListener(SWT.Selection, new Listener() {

                            public void handleEvent(Event event) {

                                     if (event.detail == SWT.ARROW) {

                                               Rectangle bounds = itemDropDown.getBounds();

                                               Point point = toolBar.toDisplay(bounds.x, bounds.y

                                                                 + bounds.height);

                                               //设置菜单的显示位置

                                               menu.setLocation(point);

                                               menu.setVisible(true);

                                     }

                            }

                   });

        //设置工具项的事件监听器

                   Listener selectionListener = new Listener() {

                            public void handleEvent(Event event) {

                                     ToolItem item = (ToolItem) event.widget;

                                     System.out.println(item.getText() + " is selected");

                                     if ((item.getStyle() & SWT.RADIO) != 0

                                                        || (item.getStyle() & SWT.CHECK) != 0)

                                               System.out.println("Selection status: "

                                                                 + item.getSelection());

                            }

                   };

                   itemPush.addListener(SWT.Selection, selectionListener);

                   itemCheck.addListener(SWT.Selection, selectionListener);

                   itemRadio1.addListener(SWT.Selection, selectionListener);

                   itemRadio2.addListener(SWT.Selection, selectionListener);

                   itemDropDown.addListener(SWT.Selection, selectionListener);

 

                   toolBar.pack();

                   shell.addListener(SWT.Resize, new Listener() {

                            public void handleEvent(Event event) {

                                     Rectangle clientArea = shell.getClientArea();

                                     toolBar.setSize(toolBar.computeSize(clientArea.width,

                                                        SWT.DEFAULT));

                            }

                   });

                   shell.setSize(400, 100);

                   shell.open();

                  

                   while (!shell.isDisposed()) {

                            if (!display.readAndDispatch()) {

                                     display.sleep();

                            }

                   }

 

                   display.dispose();

         }

 

         public static void main(String[] args) {

                   new ToolBarExample();

         }

}

程序添加了工具栏,并在工具栏中添加了相应的工具项,工具项中添加了相应的事件响应机制,程序运行效果如图2所示。

13081368_200806110859242.jpg

图2  工具栏组件

本示例显示了工具栏和菜单栏的配合使用,菜单动态设定显示的位置。

工具栏组件CoolBar和CoolItem

CoolBar是另外一种形式的工具栏,它能够调整CoolItem的位置,每一个CoolItem可以设定相关的组件(也可以是另一个工具栏),创建CoolBar的步骤如下:

1.  创建CoolBar对象,并指定创建的样式,例如“CoolBar composite = new CoolBar  (parent, SWT.NONE);”。

2.  创建CoolItem对象,并指定创建样式,例如“CoolItem item = new CoolItem(composite, SWT.NONE);”。

3.  设置CoolItem的Control对象,例如“item.setControl(tb);”。

CoolBar相当于一个面板容器,CoolItem是容器中的每一项,CoolItem能设置相应的组件作为此项的子容器(也可以是其他组件)。为了更好地掌握CoolBar组件,下面通过一个实例演示如何创建CoolBar组件,代码如例程3所示。

例程3  CoolBarExample.java

public class CoolBarExample extends ApplicationWindow {

           public CoolBarExample() {

             super(null);

           }

 

           protected Control createContents(Composite parent) {

             getShell().setText("CoolBar Test");

 

             String asCoolItemSection[] = { "File", "Formatting", "Search" };

             //添加CoolBar

             CoolBar composite = new CoolBar(parent, SWT.NONE);

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

               CoolItem item = new CoolItem(composite, SWT.NONE);

               //添加子组件

               ToolBar tb = new ToolBar(composite, SWT.FLAT);

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

                 ToolItem ti = new ToolItem(tb, SWT.NONE);

                 ti

                     .setText(asCoolItemSection[idxCoolItem] + " Item #"

                         + idxItem);

               }

               Point p = tb.computeSize(SWT.DEFAULT, SWT.DEFAULT);

               tb.setSize(p);

               Point p2 = item.computeSize(p.x, p.y);

              

               //设置为一个CoolItem的控制类

               item.setControl(tb);

               item.setSize(p2);

             }

             return composite;

           }

 

           public static void main(String[] args) {

             CoolBarExample app = new CoolBarExample();

             app.setBlockOnOpen(true);

             app.open();

             Display.getCurrent().dispose();

           }

     }

以上代码演示了如何创建CoolBar。CoolBar中每一个CoolItem可以根据用户的需要调整位置,程序运行效果如图3所示。

13081368_200806110859243.jpg

图3  CoolBar组件

CoolBar和ToolBar的展现样式不一样,CoolBar可以动态调整工具栏的位置。

滚动组件Slider

为了方便用户输入数据,SWT中提供了Slider组件,用户可通过Slider设置数据的增量值,用来控制其他组件,也可以作为滚动条控制其他组件中的数据显示。添加Slider组件的步骤如下:

1.  创建Slider对象,并指定创建的样式,例如“Slider slide = new Slider(shell, SWT.HORIZONTAL);”。

2.  设置Slider的最大值和最小值,例如“slide.setMaximum(100);”。

3.  设置Slider增量的增加或递减值,例如“slide.setIncrement(1);”。

4.  添加Slider的事件监听器,例如“slide.addSelectionListener(selectionListener);”。

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

例程4  SliderExample.java

public class SliderExample {

 

         Display dispaly;

 

         Shell shell;

 

         SliderExample() {

                   dispaly = new Display();

                   shell = new Shell(dispaly);

                   shell.setSize(300, 250);

                   shell.setText("A Slider Example");

                   //添加Slider对象

                   final Slider slide = new Slider(shell, SWT.V_SCROLL);

                   //设置Slider的位置和大小

                   slide.setBounds(170, 25, 25, 20);

                   //设置Slider的最小值

                   slide.setMinimum(0);

                   //设置Slider的最大值        

                   slide.setMaximum(100);

                   //设置Slider单击左右箭头的增加或递减值

                   slide.setIncrement(1);

 

                   final Text t = new Text(shell, SWT.BORDER);

                   t.setBounds(115, 25, 55, 20);

                   t.setText("0");

                   t.setFocus();

 

                   //添加Slider的事件监听器

                   slide.addSelectionListener(new SelectionAdapter() {

                            public void widgetSelected(SelectionEvent e) {

                                     t.setText(new Integer(slide.getSelection()).toString());

                            }

                   });

 

                   shell.open();

                   while (!shell.isDisposed()) {

                            if (!dispaly.readAndDispatch())

                                     dispaly.sleep();

                   }

                   dispaly.dispose();

         }

 

         public static void main(String[] args) {

                   new SliderExample();

         }

 

}

以上代码添加了一个Text组件和一个Slider组件。Slider组件设置了增量值为1,另外Slider组件添加了选择事件,当选择了Slider组件后,Slider将为Text组件赋值。程序运行效果如图4所示。

13081368_200806110859244.jpg

图4  Slider组件

Slider组件要配合其它的组件使用,辅助其它的组件完成功能。

刻度组件Scale

Scale和Slider类似,在SWT中都表示一种尺度,但两者的表现形式不一样,Scale更像一个刻度,而Slider则是提供一个滚动条。添加Scale组件的步骤如下:

1.  创建Scale对象,并指定创建的样式,例如“Scale scale = new Scale(shell, SWT.VERTICAL);”。

2.  设置Scale的最大值和最小值,例如“scale.setMaximum(20);”。

3.  设置Scale增量的增加或递减值,例如“scale.setPageIncrement(5);”。

4.  添加Scale的事件监听器,例如“scale.addSelectionListener(selectionListener);”。

为了更好地掌握Scale组件,下面通过一个实例演示如何创建Scale组件,代码如例程5所示。

例程5  ScaleExample.java

public class ScaleExample {

           Display display = new Display();

           Shell shell = new Shell(display);

 

           Scale scale;

           Text value;

           public ScaleExample() {

             shell.setLayout(new GridLayout(1, true));

             Label label = new Label(shell, SWT.NULL);

             label.setText("Volume:");

             //添加Scale组件

             scale = new Scale(shell, SWT.VERTICAL);

             scale.setBounds(0, 0, 40, 200);

             //设置Scale的最大值

             scale.setMaximum(20);

             //设置Scale的最小值  

             scale.setMinimum(0);

             //设置Scale的增量值

             scale.setPageIncrement(5);

             //添加Scale的事件监听器

             scale.addListener(SWT.Selection, new Listener() {

               public void handleEvent(Event event) {

                 int perspectiveValue = scale.getMaximum() - scale.getSelection() +
                 scale.getMinimum();

                 value.setText("Vol: " + perspectiveValue);

               }

             });

             value = new Text(shell, SWT.BORDER | SWT.SINGLE);

 

             value.setEditable(false);

             scale.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));

             value.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));

 

             shell.pack();

             shell.open();

             while (!shell.isDisposed()) {

               if (!display.readAndDispatch()) {

                 display.sleep();

               }

             }

 

             display.dispose();

           }

 

           private void init() {

 

           }

 

           public static void main(String[] args) {

             new ScaleExample();

           }

     }

上例中,通过事件监听器监听当前选择的刻度,并用Text组件显示出来,程序运行效果如图5所示。

13081368_200806110859245.jpg

5  Scale组件

Scale组件能够精确的显示刻度,用户可以设制好刻度的范围,这是非常有用的。

进度条组件ProgressBar

ProgressBarSWT中的进度条组件。进度条提供了比较长时间操作的进度信息。添加ProgressBar组件的步骤如下:

1.  创建ProgressBar对象,并指定创建的样式,例如“ProgressBar pb1 = new ProgressBar (shell, SWT.HORIZONTAL | SWT.SMOOTH);”。

2.  设置ProgressBar的最大值和最小值,例如“pb1.setMaximum(30);”。

3.  在长时间的任务中设置当前进度条的进度,例如“progressBar.setSelection (progressBar.getSelection() + 1);”。

进度条能反映当前的工作进度,为了配合处理长时间的任务,进度条经常配合线程使用,以免产生阻塞影响界面的操作。为了更好地掌握fj.pngimage002.jpg

fj.pngimage004.jpg

fj.pngimage006.jpg

fj.pngimage008.jpg

fj.pngimage010.jpg

fj.pngimage012.jpg

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

转载于:http://blog.itpub.net/13081368/viewspace-343310/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值