在使用GridLayout等布局管理器的時候,對于Combo或者Text控件,可能希望手工指定其的默認寬度
在使用GridLayout布局管理器的時候,對于Combo或者Text控件,可能希望手工指定其的默認寬度。
可以為其指定GridData,并用widthHint屬性來設定其寬度。
這里有點小問題就是,要想獲得一個字符串的寬度,并沒有一個簡單的方法,我們不得不多寫幾行代碼。
Composite composite = new Composite(shell , SWT.NONE);?? //新建一個容器,并使用GridLayout布局管理器
GridLayout gridlayout = new GridLayout();
composite.setLayout(gridlayout);
Text text = new Text(composite , SWT.SINGLE);?? ?? ?? ?? ?? ?? //在該容器中放入一個Text控件
GridData griddata = new GridData();
text.setLayoutData(griddata);
GC gc = new GC(shell);?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? //利用gc.textExtend方法獲得指定字符串的寬度
Point size = gc.textExtent (str);
gc.dispose();
griddata.widthHint = size.x;?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? //設定Text控件的寬度
posted on 2006-06-02 10:41
Rendezvous with Rama 閱讀(439)
評論(0) 編輯 收藏 所屬分類:
Eclipse