本文综合了GridLayout、GridData、Composite以及Tree、TreeItem构造了一个比较复杂的应用例子。
/*---------- 构造代码 ---------*/
public class GridLayoutObject {
public void buildLayout(){
Display display = new Display();
Shell shell = new Shell(display);
GridLayout configLayout = new GridLayout();
configLayout.marginHeight = 0;
configLayout.marginWidth = 0;
shell.setLayout(configLayout);
GridData gridData;
SashForm form = new SashForm(shell,SWT.HORIZONTAL);
gridData = new GridData(GridData.FILL_BOTH);
form.setLayoutData(gridData);
Composite cLeftSide = new Composite(form, SWT.BORDER);
gridData = new GridData(GridData.FILL_BOTH);
cLeftSide.setLayoutData(gridData);
FormLayout layout = new FormLayout();
cLeftSide.setLayout(layout);
Composite cFilterArea = new Composite(cLeftSide, SWT.NONE);
cFilterArea.setLayout(new FormLayout());
final Text txtFilter = new Text(cFilterArea, SWT.BORDER);
txtFilter.setText("hi");
txtFilter.selectAll();
txtFilter.setFocus();
Label lblX = new Label(cFilterArea, SWT.WRAP);
lblX.setText("l1");
Label lblSearch = new Label(cFilterArea, SWT.NONE);
lblSearch.setText("l2");
Tree tree = new Tree(cLeftSide, SWT.NONE);
FontData[] fontData = tree.getFont().getFontData();
fontData[0].setStyle(SWT.BOLD);
Font filterFoundFont = new Font(display, fontData);
FormData formData;
formData = new FormData();
/*注意:80,代表*/
formData.bottom = new FormAttachment(100, -5);
formData.left = new FormAttachment(0, 0);
formData.right = new FormAttachment(100, 0);
cFilterArea.setLayoutData(formData);
formData = new FormData();
formData.top = new FormAttachment(0,5);
formData.left = new FormAttachment(0, 5);
lblSearch.setLayoutData(formData);
formData = new FormData();
formData.top = new FormAttachment(0,5);
formData.left = new FormAttachment(lblSearch,5);
formData.right = new FormAttachment(lblX, -3);
txtFilter.setLayoutData(formData);
formData = new FormData();
formData.top = new FormAttachment(0,5);
formData.right = new FormAttachment(100,-5);
lblX.setLayoutData(formData);
formData = new FormData();
formData.top = new FormAttachment(0, 0);
formData.left = new FormAttachment(0,0);
formData.right = new FormAttachment(100,0);
formData.bottom = new FormAttachment(cFilterArea,-1);
tree.setLayoutData(formData);
Composite cRightSide = new Composite(form, SWT.NULL);
configLayout = new GridLayout();
configLayout.marginHeight = 3;
configLayout.marginWidth = 0;
cRightSide.setLayout(configLayout);
// Header
Composite cHeader = new Composite(cRightSide, SWT.BORDER);
configLayout = new GridLayout();
configLayout.marginHeight = 3;
configLayout.marginWidth = 0;
configLayout.numColumns = 2;
configLayout.marginRight = 5;
cHeader.setLayout(configLayout);
gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
cHeader.setLayoutData(gridData);
cHeader.setBackground(display.getSystemColor(SWT.COLOR_LIST_SELECTION));
cHeader.setForeground(display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
Label lHeader = new Label(cHeader, SWT.NULL);
lHeader.setBackground(display.getSystemColor(SWT.COLOR_LIST_SELECTION));
lHeader.setForeground(display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
fontData = lHeader.getFont().getFontData();
fontData[0].setStyle(SWT.BOLD);
int fontHeight = (int)(fontData[0].getHeight() * 1.2);
fontData[0].setHeight(fontHeight);
Font headerFont = new Font(display, fontData);
lHeader.setFont(headerFont);
gridData = new GridData(GridData.VERTICAL_ALIGN_CENTER | GridData.HORIZONTAL_ALIGN_BEGINNING);
lHeader.setLayoutData(gridData);
Label usermodeHint = new Label(cHeader, SWT.NULL);
usermodeHint.setBackground(display.getSystemColor(SWT.COLOR_LIST_SELECTION));
usermodeHint.setForeground(display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
gridData = new GridData(GridData.VERTICAL_ALIGN_CENTER | GridData.HORIZONTAL_ALIGN_END | GridData.GRAB_HORIZONTAL);
usermodeHint.setLayoutData(gridData);
Composite cConfigSection = new Composite(cRightSide, SWT.NULL);
StackLayout layoutConfigSection = new StackLayout();
cConfigSection.setLayout(layoutConfigSection);
gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalIndent = 2;
cConfigSection.setLayoutData(gridData);
form.setWeights(new int[] {50,80});
Listener scResizeListener = new Listener() {
public void handleEvent(Event event) {
setupSC((ScrolledComposite)event.widget);
}
};
ScrolledComposite sc = new ScrolledComposite(cConfigSection, SWT.H_SCROLL | SWT.V_SCROLL);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setLayoutData(new GridData(GridData.FILL_BOTH));
sc.getVerticalBar().setIncrement(16);
sc.addListener(SWT.Resize, scResizeListener);
ModelConfigSection a = new ModelConfigSection();
Composite c = a.configSectionCreate(sc);
sc.setContent(c);
TreeItem item1 = new TreeItem(tree,SWT.NULL);
item1.setText("说明");
item1.setData("Panel", sc);
layoutConfigSection.topControl = sc;
ScrolledComposite sc1 = new ScrolledComposite(cConfigSection, SWT.H_SCROLL | SWT.V_SCROLL);
sc1.setExpandHorizontal(true);
sc1.setExpandVertical(true);
sc1.setLayoutData(new GridData(GridData.FILL_BOTH));
sc1.getVerticalBar().setIncrement(16);
sc1.addListener(SWT.Resize, scResizeListener);
TreeItem item2 = new TreeItem(tree,SWT.NULL);
item2.setText("网络配置");
item2.setData("Panel",sc1);
ScrolledComposite sc2 = new ScrolledComposite(cConfigSection, SWT.H_SCROLL | SWT.V_SCROLL);
sc2.setExpandHorizontal(true);
sc2.setExpandVertical(true);
sc2.setLayoutData(new GridData(GridData.FILL_BOTH));
sc2.getVerticalBar().setIncrement(16);
sc2.addListener(SWT.Resize, scResizeListener);
TreeItem item21 = new TreeItem(item2,SWT.NULL);
item21.setText("路由配置");
item21.setData("Panel", sc2);
ScrolledComposite sc3 = new ScrolledComposite(cConfigSection, SWT.H_SCROLL | SWT.V_SCROLL);
sc3.setExpandHorizontal(true);
sc3.setExpandVertical(true);
sc3.setLayoutData(new GridData(GridData.FILL_BOTH));
sc3.getVerticalBar().setIncrement(16);
sc3.addListener(SWT.Resize, scResizeListener);
TreeItem item22 = new TreeItem(item2,SWT.NULL);
item22.setText("网关配置");
item22.setData("Panel", sc3);
TreeItem[] items = { tree.getItems()[0] };
tree.setSelection(items);
shell.pack();
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
public void setupSC(ScrolledComposite sc) {
Composite c = (Composite) sc.getContent();
if (c != null) {
Point size1 = c.computeSize(sc.getClientArea().width, SWT.DEFAULT);
Point size = c.computeSize(SWT.DEFAULT, size1.y);
sc.setMinSize(size);
}
sc.getVerticalBar().setPageIncrement(sc.getSize().y);
}
}
/*---------- 动作控制接口以及实现代码 --------*/
public interface ConfigSectionTest {
public Composite configSectionCreate(final Composite parent);
}
public class ModelConfigSection implements ConfigSectionTest {
public Composite configSectionCreate(Composite parent) {
GridData gridData;
GridLayout layout;
final Composite cMode = new Composite(parent,SWT.WRAP);
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL|GridData.VERTICAL_ALIGN_FILL);
cMode.setLayoutData(gridData);
layout = new GridLayout();
layout.numColumns =4;
layout.marginHeight = 0;
cMode.setLayout(layout);
gridData = new GridData();
gridData.horizontalSpan = 4;
final Group gRadio = new Group(cMode, SWT.WRAP);
gRadio.setText("用户熟练程度");
gRadio.setLayoutData(gridData);
gRadio.setLayout(new RowLayout(SWT.HORIZONTAL));
Button button0 = new Button (gRadio, SWT.RADIO);
button0.setText("初级");
Button button1 = new Button (gRadio, SWT.RADIO);
button1.setText("中级");
Button button2 = new Button (gRadio, SWT.RADIO);
button2.setText("高级");
button0.setSelection(true);
gridData = new GridData(GridData.FILL_HORIZONTAL);
final Label label = new Label(cMode, SWT.WRAP);
gridData.horizontalSpan = 4;
label.setLayoutData(gridData);
label.setText("什么玩意!");
return cMode;
}
}
/*----------- 测试代码 ----------------*/
public class GridLayoutTest {
public static void main(String[] args){
GridLayoutObject obj = new GridLayoutObject();
obj.buildLayout();
}
}
运行结果:
posted on 2009-08-13 17:27
zhangxl 阅读(2488)
评论(0) 编辑 收藏 所属分类:
SWT、SWING、AWT