锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
/*---------- 鏋勯犱唬鐮?---------*/
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();
/*娉ㄦ剰錛?0錛屼唬琛?/
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);
}
}
/*---------- 鍔ㄤ綔鎺у埗鎺ュ彛浠ュ強瀹炵幇浠g爜 --------*/
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;
}
}
/*----------- 嫻嬭瘯浠g爜 ----------------*/
public class GridLayoutTest {
public static void main(String[] args){
GridLayoutObject obj = new GridLayoutObject();
obj.buildLayout();
}
}
榪愯緇撴灉錛?br />
Menu寮忔牱
SWT.BAR 錛?鑿滃崟鏍忥紝鐢ㄤ簬涓昏彍鍗曘?br />
SWT.DROP_DOWN 錛?涓嬫媺鑿滃崟鐢ㄤ簬瀛愯彍鍗曘?br />
SWT.POP_UP 錛?nbsp; 榧犳爣鍙抽敭寮瑰嚭寮忚彍鍗曘?br />
MenuItem寮忔牱
SWT.CASCADE 錛?鏈夊瓙鑿滃崟鐨勮彍鍗曢」銆?br />
SWT.CHECK 錛?閫変腑鍚庡墠闈㈠甫灝忛挬銆?br />
SWT.PUSH 錛?nbsp; 鏅氬瀷鑿滃崟欏廣?br />
SWT.RADIO 錛?nbsp; 閫変腑鍚庡墠闈㈠甫涓灝忕偣銆?br />
SWT.SEPERATOR 錛?---- 鍒嗛殧絎?
婕旂ず浠g爜
public class MethodUtil {
public void buildMenus(){
Display display = new Display();
Shell shell = new Shell(display);
Label label = new Label(shell,SWT.BORDER|SWT.RIGHT);
label.setBounds(10, 10, 120, 140);
label.setText("榪欓噷鏄爣絳?);
label.setFont(new Font(display,"瀹嬩綋",14,SWT.BOLD));
label.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));
//榪欓噷鎸囧畾鏄富鑿滃崟錛屼笖鑿滃崟鏂瑰悜鏄粠鍙沖埌宸︽帓鍒?br />
Menu mainMenu = createMenu(shell, SWT.BAR|SWT.RIGHT_TO_LEFT);
shell.setMenuBar(mainMenu);
MenuItem fileMenuItem = createMenuItem(mainMenu, SWT.CASCADE, "鏂囦歡&F", null, SWT.CONTROL+'f', true, null);
Menu fileMenu = createMenu(shell, SWT.DROP_DOWN, fileMenuItem, true);
MenuItem createItem = createMenuItem(fileMenu, SWT.CASCADE, "鏂板緩&C", null, SWT.CONTROL+'c', true, null);
Menu createMenu = createMenu(shell, SWT.DROP_DOWN, createItem, true);
/*閲囩敤鍙嶅皠鏈哄埗璋冪敤璋冪敤浜嬩歡澶勭悊鏂規硶*/
Class[] c = new Class[1];
Object[] o = new Object[1];
c[0] = Label.class;
o[0] = label;
MenuItem createProject = createMenuItem(createMenu, SWT.PUSH, "欏圭洰\tControl+Shit+N", null, SWT.CONTROL+SWT.SHIFT+'N', true, "changeContent",c,o);
MenuItem createPackege = this.createMenuItem(createMenu, SWT.PUSH, "鍖?amp;I", null, SWT.CONTROL+'I', true, null);
MenuItem createClass = this.createMenuItem(createMenu, SWT.PUSH, "綾?amp;K", null, SWT.CONTROL+'K', true, null);
MenuItem open = this.createMenuItem(fileMenu, SWT.PUSH, "鎵撳紑&S", null, SWT.CONTROL+'S', true, null);
MenuItem exit = this.createMenuItem(fileMenu, SWT.PUSH, "鍏抽棴&S", null, SWT.CONTROL+'M', true, null);
MenuItem help = this.createMenuItem(mainMenu, SWT.PUSH, "甯姪&H", null, SWT.CONTROL+'H', true, null);
shell.pack();
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
public void changeContent(Label label){
label.setText("鎴戝彉錛屾垜鍙橈紝鎴戝彉鍙樺彉!鍒涘緩宸ョ▼!");
}
protected Menu createMenu(Menu parent, boolean enabled) {
Menu m = new Menu(parent);
m.setEnabled(enabled);
return m;
}
protected Menu createMenu(MenuItem parent, boolean enabled) {
Menu m = new Menu(parent);
m.setEnabled(enabled);
return m;
}
protected Menu createMenu(Shell parent, int style) {
Menu m = new Menu(parent, style);
return m;
}
protected Menu createMenu(Shell parent, int style,
MenuItem container, boolean enabled) {
Menu m = createMenu(parent, style);
m.setEnabled(enabled);
container.setMenu(m);
return m;
}
protected Menu createPopupMenu(Shell shell) {
Menu m = new Menu(shell, SWT.POP_UP);
shell.setMenu(m);
return m;
}
protected Menu createPopupMenu(Shell shell, Control owner) {
Menu m = createPopupMenu(shell);
owner.setMenu(m);
return m;
}
protected MenuItem createMenuItem(Menu parent, int style, String text,
Image icon, int accel, boolean enabled,
String callback,Class[] c,Object[] o){
MenuItem mi = new MenuItem(parent, style);
if (text != null) {
mi.setText(text);
}
if (icon != null) {
mi.setImage(icon);
}
if (accel != -1) {
mi.setAccelerator(accel);
}
mi.setEnabled(enabled);
if (callback != null) {
registerCallback(mi, this, callback,c,o);
}
return mi;
}
protected MenuItem createMenuItem(Menu parent, int style, String text,
Image icon, int accel, boolean enabled,
String callback) {
MenuItem mi = new MenuItem(parent, style);
if (text != null) {
mi.setText(text);
}
if (icon != null) {
mi.setImage(icon);
}
if (accel != -1) {
mi.setAccelerator(accel);
}
mi.setEnabled(enabled);
if (callback != null) {
registerCallback(mi, this, callback);
}
return mi;
}
protected void registerCallback(final MenuItem mi,
final Object handler,
final String handlerName) {
mi.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
try {
Method m = handler.getClass().getMethod(handlerName, null);
m.invoke(handler, null);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
});
}
protected void registerCallback(final MenuItem mi,final Object handler,final String handlerName,final Class[] cs,final Object[] params){
mi.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
try {
Method m = handler.getClass().getMethod(handlerName, cs);
m.invoke(handler, params);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
});
}
}
/*璋冪敤嫻嬭瘯*/
public class MenuTestDesign {
public static void main(String[] arg){
MethodUtil u = new MethodUtil();
u.buildMenus();
}
}
榪愯緇撴灉錛?br />