public void createpartcontrol(composite parent) {   toolkit = new formtoolkit(parent.getdisplay());   form = toolkit.createform(parent);   form.settext("hello, eclipse forms");   gridlayout layout = new gridlayout();   form.getbody().setlayout(layout);   hyperlink link = toolkit.createhyperlink(form.getbody(),      "click here.", swt.wrap);   link.addhyperlinklistener(new hyperlinkadapter() {    public void linkactivated(hyperlinkevent e) {     system.out.println("link activated!");    }   });  }  form的body是標(biāo)題下面的可用空間,因為這個空間是一個swt composite,它能做為其它組件的parent。在上面的代碼里,我們?yōu)閎ody設(shè)置了layout,然后創(chuàng)建了一個超鏈接。超鏈接是由eclipse forms提供的為數(shù)不多的組件之一,我們可以為超鏈接增加監(jiān)聽器,這樣能夠在用戶點擊它時做出反應(yīng)。
  layout.numcolumns = 2;   griddata gd = new griddata();   gd.horizontalspan = 2;   link.setlayoutdata(gd);   label label = new label(form.getbody(), swt.null);   label.settext("text field label:");   text text = new text(form.getbody(), swt.border);   text.setlayoutdata(new griddata(griddata.fill_horizontal));   button button = new button(form.getbody(), swt.check);   button.settext("an example of a checkbox in a form");   gd = new griddata();   gd.horizontalspan = 2;   button.setlayoutdata(gd);  現(xiàn)在我們使用了兩列,并且創(chuàng)建了一個標(biāo)簽(label),一個文本框(text field)和一個復(fù)選框(checkbox).結(jié)果如下:
label label = toolkit.createlabel(form.getbody(), "text field label:"); text text = toolkit.createtext(form.getbody(), ""); text.setlayoutdata(new griddata(griddata.fill_horizontal)); button button = toolkit.createbutton(form.getbody(), "a checkbox in a form", swt.check); gd = new griddata(); gd.horizontalspan = 2; button.setlayoutdata(gd);這個視圖現(xiàn)在會看來更好些了:


control mycontrol = new mycontrol(parent); mycontrol.setdata(formtoolkit.key_draw_border, formtoolkit.text_border); // or mycontrol.setdata(formtoolkit.key_draw_border, formtoolkit.tree_border); toolkit.paintbordersfor(parent);你可以在上面這張圖中看出,象樹和表格這樣的"結(jié)構(gòu)化(structural)"的組件有和文本區(qū)域不同的邊框風(fēng)格并且你可以選擇和你的組件相符合的那個.注意用toolkit的工廠方法創(chuàng)建出的組件不需要這樣做.

 public void createpartcontrol(composite parent) {   toolkit = new formtoolkit(parent.getdisplay());   form = toolkit.createform(parent);   form.settext("hello, eclipse forms");   tablewraplayout layout = new tablewraplayout();   form.getbody().setlayout(layout);   hyperlink link = toolkit.createhyperlink(form.getbody(),"click here.", swt.wrap);   link.addhyperlinklistener(new hyperlinkadapter() {    public void linkactivated(hyperlinkevent e) {     system.out.println("link activated!");    }   });   link.settext("this is an example of a form that is much longer and will need to wrap.");   layout.numcolumns = 2;   tablewrapdata td = new tablewrapdata();   td.colspan = 2;   link.setlayoutdata(td);   label label = toolkit.createlabel(form.getbody(), "text field label:");   text text = toolkit.createtext(form.getbody(), "");   td = new tablewrapdata(tablewrapdata.fill_grab);   text.setlayoutdata(td);   button button = toolkit.createbutton(form.getbody(), "a checkbox in a form", swt.check);   td = new tablewrapdata();   td.colspan = 2;   button.setlayoutdata(td);  }我們用了griddata相同的概念.一些變量擁有不同的名字(舉個例子,colspan和rowspan,align和valign取自html table的屬性),但是你可以做相同的事--創(chuàng)建一個超鏈接和按鈕占據(jù)兩列的格子.因為空白(margins)和gridlayout是相同的,結(jié)果會看起來一樣,除了超鏈接現(xiàn)在會包裹起來:
tablewraplayout和gridlayout的一個主要的不同點是你應(yīng)該停止去計算垂直的空間.在gridlayout里,你一般會讓"不易變形的(rigid)"組件用自然的位置和大小并讓"可伸縮的(flexible)"組件去占據(jù)水平或垂直的空間.相反,tablewraplayout是從上往下工作的,并且它容下所有的組件,它的工作是完全的.占據(jù)水平空間的概念還存在(象上面展示一樣).但是,垂直方向上,你只能在單元(cell)比組件高時選擇fill單元,或選擇top,middle或bottom垂直對齊.
圖片8:一個使用tablewraplayout的form
layout.numcolumns = 3; label label; tablewrapdata td; label = toolkit.createlabel(form.getbody(), "some text to put in the first column", swt.wrap); label = toolkit.createlabel(form.getbody(), "some text to put in the second column and make it a bit "+ "longer so that we can see what happens with column "+ distribution. this text must be the longest so that it can "+ "get more space allocated to the columns it belongs to.", swt.wrap); td = new tablewrapdata(); td.colspan = 2; label.setlayoutdata(td); label = toolkit.createlabel(form.getbody(), "this text will span two rows and should not grow the column.", swt.wrap); td = new tablewrapdata(); td.rowspan = 2; label.setlayoutdata(td); label = toolkit.createlabel(form.getbody(), "this text goes into column 2 and consumes only one cell", swt.wrap); label.setlayoutdata(new tablewrapdata(tablewrapdata.fill_grab)); label = toolkit.createlabel(form.getbody(), "this text goes into column 3 and consumes only one cell too", swt.wrap); label.setlayoutdata(new tablewrapdata(tablewrapdata.fill)); label = toolkit.createlabel(form.getbody(), "this text goes into column 2 and consumes only one cell", swt.wrap); label.setlayoutdata(new tablewrapdata(tablewrapdata.fill_grab)); label = toolkit.createlabel(form.getbody(), "this text goes into column 3 and consumes only one cell too", swt.wrap); label.setlayoutdata(new tablewrapdata(tablewrapdata.fill)); form.getbody().setbackground(form.getbody().getdisplay(). getsystemcolor(swt.color_widget_background));
這個創(chuàng)建了一些有不同長度文本的label.有些labels占據(jù)多列,有些占據(jù)多行.為了讓測試更加簡單,我們把form的背景設(shè)置為組件背景,這樣單元能夠更簡單看出來.當(dāng)我們運行例子,我們會得到下面的樣子:
關(guān)鍵之處在于組件最小寬度和最大寬度相比較時.相差越多,結(jié)果越明顯,會看到列中有很大的縫隙.占據(jù)的寬度是組件所需要的最小的寬度.注意第3列比第2列稍微寬一點點,這是因為第3列中的文字長度比第2列中文字要長點.如果需要閱讀布局的相關(guān)理論,可以去這里w3c recommendations for html table auto-layout.
圖片9:tablewraplayout留下的多余空間
 
 
新聞熱點
疑難解答
圖片精選