乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Eclipse開發(fā)經(jīng)典教程:展現(xiàn)組件

       ShangShujie 2008-03-28
       【IT168專稿】SWT中還有一些常用的組件,它們可以使組件更有表現(xiàn)力,且稱之為展現(xiàn)組件,它們在SWT開 發(fā)中也是不可缺少的,包括菜單組件、工具欄組件ToolBar和ToolItem、工具欄組件CoolBar和CoolItem、滾動(dòng)組件Slider、 刻度組件Scale和進(jìn)度條組件ProgressBar等。

        菜單組件

        在程序中,菜單是提供信息比較好的方式,SWT中通過Menu和MenuItem實(shí)現(xiàn)菜單和菜單項(xiàng),在程序中添加菜單的步驟如下。

       ?。?)創(chuàng)建Menu對象,并指定創(chuàng)建的樣式,例如“menuBar=newMenu(shell,SWT.BAR);”。

       ?。?)創(chuàng)建MenuItem對象,并指定創(chuàng)建樣式,例如“fileEnglishItem=newMenuItem(fileMenu,SWT.RADIO);”。

        (3)設(shè)置Menu和MenuItem的關(guān)聯(lián)(Menu中還可以有子Menu),例如“fileMenuHeader.setMenu(fileMenu);”。

       ?。?)添加MenuItem的事件監(jiān)聽器,例如“fileEnglishItem.addSelectionListener(newRadioItemListener());”。

        為了更好地掌握菜單,下面通過一個(gè)實(shí)例演示如何創(chuàng)建菜單,代碼如例程1所示。

        例程1MenuExample.java

        /**

        *為了節(jié)省篇幅,所有的import類已經(jīng)被注釋

        *讀者可以通過ctrl+shift+o快捷鍵,自動(dòng)引入所依賴的類

        *如果有問題可發(fā)郵件到ganshm@gmail.com

        **/

        publicclassMenuExample{

        Displaydisplay;

        Shellshell;

        MenumenuBar,fileMenu,editMenu;

        MenuItemfileMenuHeader,editMenuHeader;

        MenuItemfileExitItem,fileSaveItem,fileEnglishItem,fileGermanItem,

        editCopyItem;

        Texttext;

        publicMenuExample(){

        display=newDisplay();

        shell=newShell(display);

        shell.setText("MenuExample");

        shell.setSize(300,200);

        text=newText(shell,SWT.BORDER);

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

        //添加主菜單項(xiàng)

        menuBar=newMenu(shell,SWT.BAR);

        //添加一級子菜單

        fileMenuHeader=newMenuItem(menuBar,SWT.CASCADE);

        fileMenuHeader.setText("&File");

        //添加一級子菜單的菜單項(xiàng)

        fileMenu=newMenu(shell,SWT.DROP_DOWN);

        fileMenuHeader.setMenu(fileMenu);

        fileSaveItem=newMenuItem(fileMenu,SWT.PUSH);

        fileSaveItem.setText("&Save");

        fileEnglishItem=newMenuItem(fileMenu,SWT.RADIO);

        fileEnglishItem.setText("English");

        fileGermanItem=newMenuItem(fileMenu,SWT.RADIO);

        fileGermanItem.setText("German");

        fileExitItem=newMenuItem(fileMenu,SWT.PUSH);

        fileExitItem.setText("E&xit");

        editMenuHeader=newMenuItem(menuBar,SWT.CASCADE);

        editMenuHeader.setText("&Edit");

        editMenu=newMenu(shell,SWT.DROP_DOWN);

        editMenuHeader.setMenu(editMenu);

        editCopyItem=newMenuItem(editMenu,SWT.PUSH);

        editCopyItem.setText("&Copy");

        //添加菜單項(xiàng)的事件監(jiān)聽器

        fileExitItem.addSelectionListener(newMenuItemListener());

        fileSaveItem.addSelectionListener(newMenuItemListener());

        editCopyItem.addSelectionListener(newMenuItemListener());

        fileEnglishItem.addSelectionListener(newRadioItemListener());

        fileGermanItem.addSelectionListener(newRadioItemListener());

        shell.setMenuBar(menuBar);

        shell.open();

        while(!shell.isDisposed()){

        if(!display.readAndDispatch())

        display.sleep();

        }

        display.dispose();

        }

        classMenuItemListenerextendsSelectionAdapter{

        publicvoidwidgetSelected(SelectionEventevent){

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

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

        shell.close();

        }

        }

        }

        classRadioItemListenerextendsSelectionAdapter{

        publicvoidwidgetSelected(SelectionEventevent){

        MenuItemitem=(MenuItem)event.widget;

        text.setText(item.getText()+"ison.");

        }

        }

        publicstaticvoidmain(String[]args){

        MenuExamplemenuExample=newMenuExample();

        }

        }

        以上程序中添加了主菜單,并在主菜單中添加了兩個(gè)子菜單項(xiàng),子菜單項(xiàng)添加了相應(yīng)的事件響應(yīng)機(jī)制,程序運(yùn)行效果如圖1所示。

        圖1Menu\MenuItem組件

        菜單是可以級聯(lián)的,在子菜單中還能夠包含其它的菜單項(xiàng)。

        工具欄組件ToolBar和ToolItem

        ToolBar是SWT中的工具欄組件,ToolItem是工具欄中的工具項(xiàng)(一般表現(xiàn)為按鈕或分隔符,也可以是其他組件),在程序中添加工具欄的步驟如下:

        1.創(chuàng)建ToolBar對象,并指定創(chuàng)建的樣式,例如“toolBar=newToolBar(shell,SWT.FLAT|SWT.WRAP|SWT.RIGHT);”。

        2.創(chuàng)建ToolItem對象,并指定創(chuàng)建樣式,例如“ToolItemitemPush=newToolItem(toolBar,SWT.PUSH);”。

        3.設(shè)置ToolItem的圖標(biāo)和相關(guān)屬性,例如“itemPush.setImage(icon);”。

        4.添加ToolItem的事件監(jiān)聽器,例如“itemPush.addListener(SWT.Selection,selectionListener);”。

        為了更好地掌握工具欄組件,下面通過一個(gè)實(shí)例演示如何創(chuàng)建工具欄組件,代碼如例程2所示。

        例程2ToolBarExample.java

        publicclassToolBarExample{

        Displaydisplay=newDisplay();

        Shellshell=newShell(display);

        ToolBartoolBar;

        publicToolBarExample(){

        //添加工具欄

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

        //添加工具項(xiàng)

        ToolItemitemPush=newToolItem(toolBar,SWT.PUSH);

        itemPush.setText("PUSHitem");

        //設(shè)置工具項(xiàng)的顯示圖標(biāo)

        //Imageicon=newImage(shell.getDisplay(),"icons/new.gif");

        //itemPush.setImage(icon);

        ToolItemitemCheck=newToolItem(toolBar,SWT.CHECK);

        itemCheck.setText("CHECKitem");

        ToolItemitemRadio1=newToolItem(toolBar,SWT.RADIO);

        itemRadio1.setText("RADIOitem1");

        ToolItemitemRadio2=newToolItem(toolBar,SWT.RADIO);

        itemRadio2.setText("RADIOitem2");

        ToolItemitemSeparator=newToolItem(toolBar,SWT.SEPARATOR);

        finalToolItemitemDropDown=newToolItem(toolBar,SWT.DROP_DOWN);

        itemDropDown.setText("DROP_DOWNitem");

        itemDropDown.setToolTipText("Clickheretoseeadropdownmenu...");

        finalMenumenu=newMenu(shell,SWT.POP_UP);

        newMenuItem(menu,SWT.PUSH).setText("Menuitem1");

        newMenuItem(menu,SWT.PUSH).setText("Menuitem2");

        newMenuItem(menu,SWT.SEPARATOR);

        newMenuItem(menu,SWT.PUSH).setText("Menuitem3");

        //設(shè)置工具項(xiàng)的事件監(jiān)聽器

        itemDropDown.addListener(SWT.Selection,newListener(){

        publicvoidhandleEvent(Eventevent){

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

        Rectanglebounds=itemDropDown.getBounds();

        Pointpoint=toolBar.toDisplay(bounds.x,bounds.y

        +bounds.height);

        //設(shè)置菜單的顯示位置

        menu.setLocation(point);

        menu.setVisible(true);

        }

        }

        });

        //設(shè)置工具項(xiàng)的事件監(jiān)聽器

        ListenerselectionListener=newListener(){

        publicvoidhandleEvent(Eventevent){

        ToolItemitem=(ToolItem)event.widget;

        System.out.println(item.getText()+"isselected");

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

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

        System.out.println("Selectionstatus:"

        +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,newListener(){

        publicvoidhandleEvent(Eventevent){

        RectangleclientArea=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();

        }

        publicstaticvoidmain(String[]args){

        newToolBarExample();

        }

        }

        程序添加了工具欄,并在工具欄中添加了相應(yīng)的工具項(xiàng),工具項(xiàng)中添加了相應(yīng)的事件響應(yīng)機(jī)制,程序運(yùn)行效果如圖2所示。

        圖2工具欄組件

        本示例顯示了工具欄和菜單欄的配合使用,菜單動(dòng)態(tài)設(shè)定顯示的位置。

        工具欄組件CoolBar和CoolItem

        CoolBar是另外一種形式的工具欄,它能夠調(diào)整CoolItem的位置,每一個(gè)CoolItem可以設(shè)定相關(guān)的組件(也可以是另一個(gè)工具欄),創(chuàng)建CoolBar的步驟如下:

        1.創(chuàng)建CoolBar對象,并指定創(chuàng)建的樣式,例如“CoolBarcomposite=newCoolBar(parent,SWT.NONE);”。

        2.創(chuàng)建CoolItem對象,并指定創(chuàng)建樣式,例如“CoolItemitem=newCoolItem(composite,SWT.NONE);”。

        3.設(shè)置CoolItem的Control對象,例如“item.setControl(tb);”。

        CoolBar相當(dāng)于一個(gè)面板容器,CoolItem是容器中的每一項(xiàng),CoolItem能設(shè)置相應(yīng)的組件作為此項(xiàng)的子容器(也可以是其他組件)。為了更好地掌握CoolBar組件,下面通過一個(gè)實(shí)例演示如何創(chuàng)建CoolBar組件,代碼如例程3所示。

        例程3CoolBarExample.java

        publicclassCoolBarExampleextendsApplicationWindow{

        publicCoolBarExample(){

        super(null);

        }

        protectedControlcreateContents(Compositeparent){

        getShell().setText("CoolBarTest");

        StringasCoolItemSection[]={"File","Formatting","Search"};

        //添加CoolBar

        CoolBarcomposite=newCoolBar(parent,SWT.NONE);

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

        CoolItemitem=newCoolItem(composite,SWT.NONE);

        //添加子組件

        ToolBartb=newToolBar(composite,SWT.FLAT);

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

        ToolItemti=newToolItem(tb,SWT.NONE);

        ti

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

        +idxItem);

        }

        Pointp=tb.computeSize(SWT.DEFAULT,SWT.DEFAULT);

        tb.setSize(p);

        Pointp2=item.computeSize(p.x,p.y);

        //設(shè)置為一個(gè)CoolItem的控制類

        item.setControl(tb);

        item.setSize(p2);

        }

        returncomposite;

        }

        publicstaticvoidmain(String[]args){

        CoolBarExampleapp=newCoolBarExample();

        app.setBlockOnOpen(true);

        app.open();

        Display.getCurrent().dispose();

        }

        }

        以上代碼演示了如何創(chuàng)建CoolBar。CoolBar中每一個(gè)CoolItem可以根據(jù)用戶的需要調(diào)整位置,程序運(yùn)行效果如圖3所示。

        圖3CoolBar組件

        CoolBar和ToolBar的展現(xiàn)樣式不一樣,CoolBar可以動(dòng)態(tài)調(diào)整工具欄的位置。

        滾動(dòng)組件Slider

        為了方便用戶輸入數(shù)據(jù),SWT中提供了Slider組件,用戶可通過Slider設(shè)置數(shù)據(jù)的增量值,用來控制其他組件,也可以作為滾動(dòng)條控制其他組件中的數(shù)據(jù)顯示。添加Slider組件的步驟如下:

        1.創(chuàng)建Slider對象,并指定創(chuàng)建的樣式,例如“Sliderslide=newSlider(shell,SWT.HORIZONTAL);”。

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

        3.設(shè)置Slider增量的增加或遞減值,例如“slide.setIncrement(1);”。

        4.添加Slider的事件監(jiān)聽器,例如“slide.addSelectionListener(selectionListener);”。

        為了更好地掌握Slider組件,下面通過一個(gè)實(shí)例演示如何創(chuàng)建Slider組件,代碼如例程4所示。

        例程4SliderExample.java

        publicclassSliderExample{

        Displaydispaly;

        Shellshell;

        SliderExample(){

        dispaly=newDisplay();

        shell=newShell(dispaly);

        shell.setSize(300,250);

        shell.setText("ASliderExample");

        //添加Slider對象

        finalSliderslide=newSlider(shell,SWT.V_SCROLL);

        //設(shè)置Slider的位置和大小

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

        //設(shè)置Slider的最小值

        slide.setMinimum(0);

        //設(shè)置Slider的最大值

        slide.setMaximum(100);

        //設(shè)置Slider單擊左右箭頭的增加或遞減值

        slide.setIncrement(1);

        finalTextt=newText(shell,SWT.BORDER);

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

        t.setText("0");

        t.setFocus();

        //添加Slider的事件監(jiān)聽器

        slide.addSelectionListener(newSelectionAdapter(){

        publicvoidwidgetSelected(SelectionEvente){

        t.setText(newInteger(slide.getSelection()).toString());

        }

        });

        shell.open();

        while(!shell.isDisposed()){

        if(!dispaly.readAndDispatch())

        dispaly.sleep();

        }

        dispaly.dispose();

        }

        publicstaticvoidmain(String[]args){

        newSliderExample();

        }

        }

        以上代碼添加了一個(gè)Text組件和一個(gè)Slider組件。Slider組件設(shè)置了增量值為1,另外Slider組件添加了選擇事件,當(dāng)選擇了Slider組件后,Slider將為Text組件賦值。程序運(yùn)行效果如圖4所示。

        圖4Slider組件

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

        刻度組件Scale

        Scale和Slider類似,在SWT中都表示一種尺度,但兩者的表現(xiàn)形式不一樣,Scale更像一個(gè)刻度,而Slider則是提供一個(gè)滾動(dòng)條。添加Scale組件的步驟如下:

        1.創(chuàng)建Scale對象,并指定創(chuàng)建的樣式,例如“Scalescale=newScale(shell,SWT.VERTICAL);”。

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

        3.設(shè)置Scale增量的增加或遞減值,例如“scale.setPageIncrement(5);”。

        4.添加Scale的事件監(jiān)聽器,例如“scale.addSelectionListener(selectionListener);”。

        為了更好地掌握Scale組件,下面通過一個(gè)實(shí)例演示如何創(chuàng)建Scale組件,代碼如例程5所示。

        例程5ScaleExample.java

        publicclassScaleExample{

        Displaydisplay=newDisplay();

        Shellshell=newShell(display);

        Scalescale;

        Textvalue;

        publicScaleExample(){

        shell.setLayout(newGridLayout(1,true));

        Labellabel=newLabel(shell,SWT.NULL);

        label.setText("Volume:");

        //添加Scale組件

        scale=newScale(shell,SWT.VERTICAL);

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

        //設(shè)置Scale的最大值

        scale.setMaximum(20);

        //設(shè)置Scale的最小值

        scale.setMinimum(0);

        //設(shè)置Scale的增量值

        scale.setPageIncrement(5);

        //添加Scale的事件監(jiān)聽器

        scale.addListener(SWT.Selection,newListener(){

        publicvoidhandleEvent(Eventevent){

        intperspectiveValue=scale.getMaximum()-scale.getSelection()+

        scale.getMinimum();

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

        }

        });

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

        value.setEditable(false);

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

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

        shell.pack();

        shell.open();

        while(!shell.isDisposed()){

        if(!display.readAndDispatch()){

        display.sleep();

        }

        }

        display.dispose();

        }

        privatevoidinit(){

        }

        publicstaticvoidmain(String[]args){

        newScaleExample();

        }

        }

        上例中,通過事件監(jiān)聽器監(jiān)聽當(dāng)前選擇的刻度,并用Text組件顯示出來,程序運(yùn)行效果如圖5所示。

        圖5Scale組件

        Scale組件能夠精確的顯示刻度,用戶可以設(shè)制好刻度的范圍,這是非常有用的。

        進(jìn)度條組件ProgressBar

        ProgressBar是SWT中的進(jìn)度條組件。進(jìn)度條提供了比較長時(shí)間操作的進(jìn)度信息。添加ProgressBar組件的步驟如下:

        1.創(chuàng)建ProgressBar對象,并指定創(chuàng)建的樣式,例如“ProgressBarpb1=newProgressBar(shell,SWT.HORIZONTAL|SWT.SMOOTH);”。

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

        3.在長時(shí)間的任務(wù)中設(shè)置當(dāng)前進(jìn)度條的進(jìn)度,例如“progressBar.setSelection(progressBar.getSelection()+1);”。

        進(jìn)度條能反映當(dāng)前的工作進(jìn)度,為了配合處理長時(shí)間的任務(wù),進(jìn)度條經(jīng)常配合線程使用,以免產(chǎn)生阻塞影響界面的操作。為了更好地掌握ProgressBar組件,下面通過一個(gè)實(shí)例演示如何創(chuàng)建ProgressBar組件,代碼如例程6所示。

        例程6ProgressBarExample.java

        publicclassProgressBarExample{

        publicstaticvoidmain(String[]args){

        Displaydisplay=newDisplay();

        Shellshell=newShell(display);

        shell.setLayout(newGridLayout());

        //添加平滑的進(jìn)度條

        ProgressBarpb1=newProgressBar(shell,SWT.HORIZONTAL|SWT.SMOOTH);

        pb1.setLayoutData(newGridData(GridData.FILL_HORIZONTAL));

        //設(shè)置進(jìn)度條的最小值

        pb1.setMinimum(0);

        //設(shè)置進(jìn)度條的最大值

        pb1.setMaximum(30);

        //添加自動(dòng)遞增的進(jìn)度條

        ProgressBarpb2=newProgressBar(shell,SWT.HORIZONTAL|

        SWT.INDETERMINATE);

        pb2.setLayoutData(newGridData(GridData.FILL_HORIZONTAL));

        //添加線程,在線程中處理長時(shí)間的任務(wù),并最終反映在平滑進(jìn)度條上

        newLongRunningOperation(display,pb1).start();

        shell.open();

        while(!shell.isDisposed()){

        if(!display.readAndDispatch()){

        display.sleep();

        }

        }

        }

        }

        classLongRunningOperationextendsThread{

        privateDisplaydisplay;

        privateProgressBarprogressBar;

        publicLongRunningOperation(Displaydisplay,ProgressBarprogressBar){

        this.display=display;

        this.progressBar=progressBar;

        }

        publicvoidrun(){

        //模仿長時(shí)間的任務(wù)

        for(inti=0;i<30;i++){

        try{

        Thread.sleep(1000);

        }catch(InterruptedExceptione){

        }

        display.asyncExec(newRunnable(){

        publicvoidrun(){

        if(progressBar.isDisposed())return;

        //進(jìn)度條遞增

        progressBar.setSelection(progressBar.getSelection()+1);

        }

        });

        }

        }

        }

        以上代碼添加了兩個(gè)進(jìn)度條,一個(gè)進(jìn)度條為自動(dòng)顯示增加進(jìn)度的信息(SWT.INDETERMINAT樣式),另外一個(gè)進(jìn)度條通過線程處理長時(shí)間的任務(wù),并設(shè)定進(jìn)度條的信息。程序運(yùn)行效果如圖6所示。

        圖6ProgressBar組件

        進(jìn)度條有不同的樣式,在程序中,開發(fā)人員可以控制進(jìn)度條的進(jìn)度,執(zhí)行某些長時(shí)間的操作。

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多