swt_jface(2) 幾種常用布局
// RowLayoutTest .java
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class RowLayoutTest {
/**
* @param args
*/
public static void main(String[] args) {
Display display=new Display();
Shell shell=new Shell();
shell.setText("標(biāo)題");
shell.setSize(400,300);
//RowLayout布局就是是組件按行排列
//shell.setLayout(new RowLayout());
//組件
new Button(shell,SWT.NONE).setText("kkk");
new Button(shell, SWT.NONE).setText("確定");
new Button(shell,SWT.NONE).setText("kk1");
new Button(shell, SWT.NONE).setText("確1");
new Button(shell,SWT.NONE).setText("kk2");
new Button(shell, SWT.NONE).setText("確2");
new Button(shell,SWT.NONE).setText("kkk");
new Button(shell, SWT.NONE).setText("確定");
new Button(shell,SWT.NONE).setText("kk1");
new Button(shell, SWT.NONE).setText("確1");
new Button(shell,SWT.NONE).setText("kk2");
new Button(shell, SWT.NONE).setText("確2");
new Button(shell,SWT.NONE).setText("kkk");
new Button(shell, SWT.NONE).setText("確定");
new Button(shell,SWT.NONE).setText("kk1");
new Button(shell, SWT.NONE).setText("確1");
new Button(shell,SWT.NONE).setText("kk2");
new Button(shell, SWT.NONE).setText("確2");
RowLayout layout = new RowLayout( /*SWT.VERTICAL垂直排列*/ );
layout.marginWidth = 20;
layout.marginHeight = 10;
layout.spacing = 15;
//垂直排列
//layout.type = SWT.VERTICAL;
//設(shè)置布局管理器上的組件大小相同
//layout.pack = false;
// 設(shè)置布局管理器上的組件根據(jù)容器空間可以拉伸
layout.justify = true;
//不自動(dòng)換行
// layout.wrap = false;
new Button(shell, SWT.NONE).setText("確定");
Button b = new Button(shell, SWT.NONE);
b.setText("取消");
// 使用RowData布局?jǐn)?shù)據(jù)類來控制按鈕,使按鈕改為50像素寬,30像素長
RowData rowData = new RowData(50, 30);
// 把組件隱藏不占位,相當(dāng)于組件不存在
//rowData.exclude = true;
rowData.width = 100;
//把組件隱藏,但位置還占著
//b.setVisible(false);
b.setLayoutData(rowData);
new Button(shell, SWT.NONE).setText("幫助");
shell.setLayout(layout);
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
}
//GridLayoutTest
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutTest {
/**
* @param args
*/
/**
* @param args
*/
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell();
shell.setText("標(biāo)題");
shell.setSize(400, 300);
//GridLayout為網(wǎng)格布局
//4代表把這個(gè)布局分成幾列
//false代表是否等距分隔空間
shell.setLayout(new GridLayout(4, true));
// 組建
new Button(shell, SWT.NONE).setText("kkk");
new Button(shell, SWT.NONE).setText("確定");
new Button(shell, SWT.NONE).setText("kk1");
new Button(shell, SWT.NONE).setText("確1");
new Button(shell,SWT.NONE).setText("kk2");
new Button(shell, SWT.NONE).setText("確2");
new Button(shell, SWT.NONE).setText("kkk");
new Button(shell, SWT.NONE).setText("確定");
new Button(shell, SWT.NONE).setText("kk1");
new Button(shell, SWT.NONE).setText("確1");
new Button(shell,SWT.NONE).setText("kk2");
new Button(shell, SWT.NONE).setText("確2");
// 定義一個(gè)GridData對象,讓幫助按鈕占用n列的空間
Button helpButton = new Button(shell, SWT.NONE);
//GridData.FILL_VERTICAL是按鈕垂直放置
GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL/*這個(gè)是水平對齊式填充*//* GridData.FILL_HORIZONTAL 這個(gè)是水平搶占式填充*//* GridData.FILL_VERTICAL*/);
gridData.horizontalSpan = 3;//該句使按鈕占用兩列空間
//gridData.grabExcessHorizontalSpace=true;
//gridData.verticalSpan = 2;
//gridData.grabExcessVerticalSpace=true;
helpButton.setLayoutData(gridData);
helpButton.setText("幫助");
new Button(shell, SWT.NONE).setText("kk1");
new Button(shell, SWT.NONE).setText("確1");
new Button(shell,SWT.NONE).setText("kk2");
new Button(shell, SWT.NONE).setText("確2");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
//FillLayoutTestimport org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FillLayoutTest {
public static void main(String[] args) {
Display display=new Display();
Shell shell=new Shell();
shell.setText("標(biāo)題");
shell.setSize(400,300);
//布局FillLayout就是使組建占滿整個(gè)容器 默認(rèn)為橫著排 SWT.VERTICAL是組建垂直排
// shell.setLayout(new FillLayout(SWT.VERTICAL));
shell.setLayout(new FillLayout());
//組建
new Button(shell,SWT.NONE).setText("kkk");
new Button(shell, SWT.NONE).setText("確定");
new Button(shell,SWT.NONE).setText("kk1");
// new Button(shell, SWT.NONE).setText("確1");
// new Button(shell,SWT.NONE).setText("kk2");
// new Button(shell, SWT.NONE).setText("確2");
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
}
posted on 2009-12-03 08:17 javaz 閱讀(984) 評論(0) 編輯 收藏 所屬分類: javaSE