国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

Eclipse Forms設(shè)計漂亮UI之布局

2024-07-21 02:14:52
字體:
供稿:網(wǎng)友
  增加一些內(nèi)容

  現(xiàn)在我們已經(jīng)有一個view能夠成功運行了.我們可以往里面增加一些內(nèi)容.eclipse forms有一個body,我們可以這樣創(chuàng)建內(nèi)容.
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)。

  升級后的視圖應(yīng)該看起來象這樣:



圖3:一個有超鏈接的簡單form.

  超鏈接組(hyperlink groups)

  form tookit有一個"超鏈接組"對象.每個創(chuàng)建出的超鏈接都加入這個組對象中.超鏈接為多個角色服務(wù).它們定義了這個組中所有超鏈接在正常、hover、激活不同狀態(tài)下的顏色.它們根據(jù)小組中鏈接不同的狀態(tài)來改變顏色.它們根據(jù)小組中鏈接不同的狀態(tài)來改變下劃線風(fēng)格.

  當(dāng)你要改變超鏈接組對象的默認(rèn)設(shè)置時,可以通過toolkit的gethyperlinkgroup()方法來獲得超鏈接組對象.

  創(chuàng)建普通組件

  eclipse forms的一個設(shè)計目標(biāo)就是讓能夠在編輯器/視圖中創(chuàng)建普通swt組件.因為form的body是一個普通composite,你能夠在它里面使用任何layout和組件.但是,記住"原生的"swt組件有一個組件背景.我們現(xiàn)在通過它們的構(gòu)造方法創(chuà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é)果如下:


圖4:一個擁有直接用它們的構(gòu)造器創(chuàng)建出的swt組件的form

   這張圖片怎么回事?我們創(chuàng)建的組件的背景直接和系統(tǒng)窗口背景相匹配,而不是和form的背景匹配.另外,文本框看起來還好是因為這張截圖是在windows xp下截的.在其它操作系統(tǒng)上,它會看起來是有一個3d邊框的空白條.為了解決這個問題,我們會用toolkit的工廠方法來創(chuàng)建這些組件:
  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)在會看來更好些了:


圖5:一個擁有用form toolkit的工廠方法創(chuàng)建出的swt組件的form

  由form toolkit提供的工廠方法是為了方便。toolkit沒有函蓋所有情況,甚至是swt組件集合,而且明顯沒有為你可能有的自己定制的組件提供這一便利.當(dāng)你需要使一個swt組件與form統(tǒng)一時,你應(yīng)該使用一個方法:formtoolkit.adapt(control control, boolean trackfocus,boolean trackkeyboard).所有的工廠方法各自分別調(diào)用了這個適配方法。

  達(dá)到"平滑"的視覺效果

  一個在pde編輯器中eclipse forms的可看出的屬性是組件的"平滑"視覺效果.以前所有沒有3d邊框的組件在窗口中看起來不錯,但是在編輯器或視圖中就不行.這個支持寫在formtoolkit類中.但是,在一些系統(tǒng)上它是通過一些定制達(dá)到的.舉個例子,看一下這張從pde編輯器(2.1版本)中的截圖:



圖片6:eclipse 2.1中eclipse forms的平滑視覺效果.

  象表格,文本類,復(fù)選框等,是加上一個平滑的1個象素寬的邊框.這些邊框不是來自組件自己(swt.border風(fēng)格沒有用到).另外,如果設(shè)置一下,tookit會為每個組件的parent增加一個paint監(jiān)聽器,在paint事件發(fā)生時為組件加上邊框.要想這樣的話,你需要為你創(chuàng)建的象文本,表格,樹等組件的每個composite調(diào)用paintbordersfor(parent).每個parent只需要調(diào)用一次方法就足夠了:不需要為每個組件這樣調(diào)用.

  form toolkit知道哪個組件需要一個定制的邊框.但是,你可能新創(chuàng)建了一個不在原來名單中的組件,它也需要一個邊框.你可以通過象下面這樣的代碼給toolkit一個提示:
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)建出的組件不需要這樣做.

  因為eclipse 3.0和在windows xp上,當(dāng)javaw.exe.manifest文件在java虛擬機bin文件夾中時,沒有邊框產(chǎn)生出來.(這篇文章里我所有截圖都是在windows xp下截的).這個文件--你可以在 swt home page上下載--告訴toolkit為本地組件使用xp皮膚.用xp皮膚,象文本,表格和樹是已經(jīng)平滑的并且不需要再去調(diào)整.為了讓你的代碼擁有可移植性,你可以總是調(diào)用paintbordersfor(composite)方法,讓toolkit去根據(jù)不同操作系統(tǒng)來決定該怎么做。

  定制布局(custom layouts)

  eclipse forms在swt layout的基礎(chǔ)上增加了兩個新的layout,這些layout繼承了swt layout基類并能夠在任何swt composite上使用,但是一般都是和eclipse forms聯(lián)合使用的。

  tablewraplayout

  現(xiàn)在我們知道如何來組合一個form,讓我們先給一個懸念.我們會改變那個超鏈接文本使它加長一些:

link.settext("this is an example of a form that is much longer "+
                     "and will need to wrap.");

  讓我們看看結(jié)果:


圖片7:一個使用gridlayout的form

  發(fā)生什么事了?記住我們使用的是gridlayout,當(dāng)它問這個超鏈接組件來計算它大小時,超鏈接告訴它文字在單獨行里需要的長度,雖然我們告訴組件去包裹(wrap),它并沒有這樣做因為gridlayout需要組件返回它的長度。超連接組件--和其它象label一樣的swt組件,可以通過你傳遞它數(shù)值來決定長和寬,但是gridlayout不會向組件傳遞數(shù)值參數(shù)。

  我們需要的是一個象html表格一樣的layout.我們希望內(nèi)容去試圖配合提供的客戶空間,并且一行行地疊加.eclipse forms提供了一個這樣的layout叫tablewraplayout.gridlayout和tablewraplayout之間有許多共同點.都是用表格來組織parent的children.都有l(wèi)ayout data來告訴layout如何對待每個組件.都能夠在需要占據(jù)所有空間時等接受組件的提示.

  但是,它們在布局時完全不同.tablewraplayout從列開始.它計算每列的最小的,合適的,最大的寬度并用這個信息來占據(jù)空間.它也試圖盡可能地公平地在各列間分隔,這樣有些組件就沒有多余的空間.

  讓我們使用tablewraplayout來重新更改例子(更改處高亮顯示):
 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)在會包裹起來:
 
圖片8:一個使用tablewraplayout的form
  tablewraplayout和gridlayout的一個主要的不同點是你應(yīng)該停止去計算垂直的空間.在gridlayout里,你一般會讓"不易變形的(rigid)"組件用自然的位置和大小并讓"可伸縮的(flexible)"組件去占據(jù)水平或垂直的空間.相反,tablewraplayout是從上往下工作的,并且它容下所有的組件,它的工作是完全的.占據(jù)水平空間的概念還存在(象上面展示一樣).但是,垂直方向上,你只能在單元(cell)比組件高時選擇fill單元,或選擇top,middle或bottom垂直對齊.

  你也許會注意到一個地方和剛剛說的不符:tablewraplayout仍然有一個grabvertical變量.但是,這里這個變量在這里有明確的目的:當(dāng)一個設(shè)置了高度的組件占多行時,它的高度會給出一個條件就是垂直dimension已經(jīng)知道了,組件需要去除它所在的多個單元之間的多余空間.

  為了用tablewraplayout有好的結(jié)果,確定組件可以接近風(fēng)格(swt.wrap).eclipse froms提供的組合定制組件能夠在box外面包裹.這是通過實現(xiàn)ilayoutextension接口實現(xiàn)的:

public interface ilayoutextension {
   /**
    * computes the minimum width of the parent. all widgets capable of word
    * wrapping should return the width of the longest word that cannot be
    * broken any further.
    *
    * @param parent the parent composite
    * @param changed <code>true</code> if the cached information should be
    * flushed, <code>false</code> otherwise.
    * @return the minimum width of the parent composite
    */
   public int computeminimumwidth(composite parent, boolean changed);

   /**
    * computes the maximum width of the parent. all widgets capable of word
    * wrapping should return the length of the entire text with wrapping
    * turned off.
    *
    * @param parent the parent composite
    * @param changed <code>true</code> if the cached information
    * should be flushed, <code>false</code> otherwise.
    * @return the maximum width of the parent composite
    */
   public int computemaximumwidth(composite parent, boolean changed);
}

  tablewraplayout本身實現(xiàn)了這個接口,這樣讓它處理當(dāng)composites的layout是這個composite的parent的children的情況.另外的兩個方法能夠計算兩種極端情況--當(dāng)所有組件盡可能寬地布滿時的顯然的最小寬度和最大寬度.兩者的不同是使列之間的多余空間盡可能小時貢獻(xiàn)出公平的空間.

  讓我們看清楚貢獻(xiàn)空間是怎樣的.我們會推薦我們到現(xiàn)在為止寫的代碼并象下面這樣做出修改:
 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)我們運行例子,我們會得到下面的樣子:

圖片9:tablewraplayout留下的多余空間
  關(guān)鍵之處在于組件最小寬度和最大寬度相比較時.相差越多,結(jié)果越明顯,會看到列中有很大的縫隙.占據(jù)的寬度是組件所需要的最小的寬度.注意第3列比第2列稍微寬一點點,這是因為第3列中的文字長度比第2列中文字要長點.如果需要閱讀布局的相關(guān)理論,可以去這里w3c recommendations for html table auto-layout.

  columnlayout

  eclipse forms另外一個定制的layout是swt rowlayout的變種.如果我們把rowlayout上的children垂直放置時--按列-并且使同列中的所有組件擁有相同的寬度,我們會由組件的寬度得到多個列.但是,最后一列顯然不是被填滿的--這由組件的個數(shù)決定.如果在form中,我們會仍然看到所有組件在一列中,因為rowlayout不能夠"垂直" 包裹.如果我們用gridlayout相替代的話,列的數(shù)量由我們自己決定.

  在更加復(fù)雜的forms中我們需要列的數(shù)量按情況變化.換句話來說,我們希望數(shù)字按照form的寬度來改變--有可能需要更多的列,當(dāng)寬度減小時則把數(shù)字減小.而且我們希望所有列的長度象報紙布局一樣相同.這些要求都能夠通過columnlayout來達(dá)到.

  與tablewraplayout相比,columnlayout更加簡單.不需要復(fù)雜的設(shè)置.唯一需要你設(shè)置的就是列數(shù)的范圍,默認(rèn)是1到3.下面的例子演示了一個使用columnlayout的有許多段落(sections)的form.初始時,只需要兩列就可以放置所有段落了.如果我們把編輯器變窄一點,layout會使它們在同一列中.

 



圖片10:使用columnlayout的所有按列排列的段落.layout開始是兩列,但是按照空間的改變變成了一列.
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 车险| 南开区| 曲水县| 聂荣县| 会理县| 北安市| 栖霞市| 柞水县| 叙永县| 芦山县| 错那县| 黄龙县| 荣昌县| 紫金县| 喀喇沁旗| 海城市| 泾阳县| 永修县| 彰化县| 民权县| 苏尼特左旗| 侯马市| 铜鼓县| 巴南区| 黄龙县| 宣汉县| 马龙县| 黄陵县| 西华县| 涡阳县| 昂仁县| 德安县| 股票| 眉山市| 盘山县| 盐亭县| 渝中区| 徐水县| 霸州市| 永仁县| 禹城市|