SWT當中的GridLayout是一個非常靈活的控制項,但是在使用起來需要在控制上下一番功夫.大家都知道,JAVA在寫編寫視窗程序
的時候,物件的添加,放置
操作起來要比.net費勁的多,但是如果用好了相關org.eclipse.layout.*包當中的相關類,也會寫出十分漂亮的介面程式.
先看下面的代碼。解釋一下,放在gridlayout中控制項是根據定義時候的順序放置的。
package ddd;<br />import org.eclipse.swt.SWT;<br />import org.eclipse.swt.layout.GridData;<br />import org.eclipse.swt.layout.GridLayout;<br />import org.eclipse.swt.layout.RowLayout;<br />import org.eclipse.swt.widgets.Button;<br />import org.eclipse.swt.widgets.Display;<br />import org.eclipse.swt.widgets.Group;<br />import org.eclipse.swt.widgets.Label;<br />import org.eclipse.swt.widgets.Monitor;<br />import org.eclipse.swt.widgets.Shell;<br />import org.eclipse.swt.widgets.Text;<br />public class TestGridLayout {<br />public static void main(String[] args) {<br />Display display = new Display();<br />Shell shell = new Shell(display);<br />shell.setText("Find (GridLayout)");<br />Label label = new Label(shell, SWT.NONE);<br />label.setText("Find what:");<br />Text text = new Text(shell, SWT.BORDER);<br />Button findButton = new Button(shell, SWT.PUSH);<br />findButton.setText("Find Next");<br />Group group = new Group(shell, SWT.NONE);<br />group.setLayout(new RowLayout());<br />Button upButton = new Button(group, SWT.RADIO);<br />upButton.setText("Up");<br />Button downButton = new Button(group, SWT.RADIO);<br />downButton.setText("Down");<br />downButton.setSelection(true);<br />group.setText("Direction");<br />Button cancelButton = new Button(shell, SWT.PUSH);<br />cancelButton.setText("Cancel");<br />/* Use a GridLayout to position the controls */<br />Monitor monitor = shell.getMonitor();<br />int width = monitor.getClientArea().width / 10;<br />GridLayout layout = new GridLayout(4, false);<br />layout.marginWidth = layout.marginHeight = 14;// layout leave's the<br />// window's space<br />shell.setLayout(layout);<br />GridData labelData = new GridData(SWT.FILL, SWT.CENTER, false, false);<br />label.setLayoutData(labelData);<br />GridData textData = new GridData(SWT.FILL, SWT.CENTER, true, false, 2,<br />1);<br />textData.widthHint = width;<br />text.setLayoutData(textData);<br />GridData findData = new GridData(SWT.FILL, SWT.CENTER, false, false);<br />findButton.setLayoutData(findData);<br />GridData groupData = new GridData(SWT.RIGHT, SWT.CENTER, false, false,<br />3, 1);<br />group.setLayoutData(groupData);<br />GridData cancelData = new GridData(SWT.FILL, SWT.CENTER, false, false);<br />cancelButton.setLayoutData(cancelData);<br />shell.pack();<br />shell.open();<br />while (!shell.isDisposed()) {<br />if (!display.readAndDispatch())<br />display.sleep();<br />}<br />display.dispose();<br />}<br />}
這其中我們在使用的時候應該要注意以下幾點:
1.要選擇自己適合
的Layout類型.GridLayout適合於多種情況,它大部分情況是使用在較為複雜的介面編程當中,因為複雜的介面會有相當多的控制項.
2.GridData
的使用將是一個控制介面顯示的主要類.通過使用GridData我們可以很好的控制介面.其中GridData的建構函式比較多,但是相關的使用我們都應該熟悉,特別是上面來源程式當中使用的那個建構函式,在使用起來更容易控制
GridLayout的布局.通過horizantalSpan,VerticalSpan來控制控制項所佔用的儲存格,這樣就會控制其它控制是否在一列當
中顯示還是在幾列當中顯示.前提是通過GridLayout.numColumns來設定列數.
3.如果不設定GridData那麼相關
的控制項都會按照相關的建立順序加入到GridLayout當中.GridData不能控制控制項的顯示順序,而相關順序是對象的建立順序來控制的.這一點不
要與GridData混淆了.