SWT(JFace)體驗(yàn)之RowLayout布局
RowLayout布局
相對(duì)于FillLayout來(lái)說(shuō),RowLayout比較靈活,功能也比較強(qiáng)。用戶可以設(shè)置布局中子元素的大小、邊距、換行及間距等屬性。
RowLayout的風(fēng)格
RowLayout中可以以相關(guān)的屬性設(shè)定布局的風(fēng)格,用戶可以通過(guò)“RowLayout.屬性”的方式設(shè)置RowLayout的布局風(fēng)格,RowLayout中常用的屬性如下。
Wrap:表示子組件是否可以換行(true為可換行)。
Pack:表示子組件是否為保持原有大小(true為保持原有大?。?。
Justify:表示子組件是否根據(jù)父組件信息做調(diào)整。
MarginLeft:表示當(dāng)前組件距離父組件左邊距的像素點(diǎn)個(gè)數(shù)。
MarginTop:表示當(dāng)前組件距離父組件上邊距的像素點(diǎn)個(gè)數(shù)。
MarginRight:表示當(dāng)前組件距離父組件右邊距的像素點(diǎn)個(gè)數(shù)。
MarginBottom:表示當(dāng)前組件距離父組件下邊距的像素點(diǎn)個(gè)數(shù)。
Spacing:表示子組件之間的間距像素點(diǎn)個(gè)數(shù)。
另外,RowLayout可以通過(guò)RowData設(shè)置每個(gè)子組件的大小,例如“button.setLayoutData (new RowData(60, 60))”將設(shè)置button的大小為(60,60)。
測(cè)試代碼:
package swt_jface.demo2;
import org.eclipse.swt.SWT;
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.List;
import org.eclipse.swt.widgets.Shell;
public class RowLayoutSample {
Display display = new Display();
Shell shell = new Shell(display);
public RowLayoutSample() {
RowLayout rowLayout = new RowLayout();
// rowLayout.fill = true;
// rowLayout.justify = true;
// rowLayout.pack = false;
// rowLayout.type = SWT.VERTICAL;
// rowLayout.wrap = false;
shell.setLayout(rowLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1");
button1.setLayoutData(new RowData(100, 35));
List list = new List(shell, SWT.BORDER);
list.add("item 1");
list.add("item 2");
list.add("item 3");
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button #2");
//shell.setSize(120, 120);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new RowLayoutSample();
}
}
再看看下面這個(gè)動(dòng)態(tài)的(結(jié)合Composite)
package swt_jface.demo2;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Layouting {
Display display = new Display();
Shell shell = new Shell(display);
int count = 0;
public Layouting() {
shell.setLayout(new RowLayout());
final Composite composite = new Composite(shell, SWT.BORDER);
composite.setLayout(new RowLayout());
composite.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
composite.addControlListener(new ControlListener() {
public void controlMoved(ControlEvent e) {
}
public void controlResized(ControlEvent e) {
System.out.println("Composite resize.");
}
});
Button buttonAdd = new Button(shell, SWT.PUSH);
buttonAdd.setText("Add new button");
buttonAdd.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Button button = new Button(composite, SWT.PUSH);
button.setText("Button #" + (count++));
composite.layout(true);
composite.pack();
shell.layout(true);
shell.pack(true);
}
});
shell.pack();
//shell.setSize(450, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new Layouting();
}
}
相關(guān)文章
SpringCache 分布式緩存的實(shí)現(xiàn)方法(規(guī)避redis解鎖的問(wèn)題)
這篇文章主要介紹了SpringCache 分布式緩存的實(shí)現(xiàn)方法(規(guī)避redis解鎖的問(wèn)題),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Mybatis的一級(jí)緩存和二級(jí)緩存原理分析與使用
mybatis-plus 是一個(gè) Mybatis 的增強(qiáng)工具,在 Mybatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開發(fā)、提高效率而生,這篇文章帶你了解Mybatis的一級(jí)和二級(jí)緩存2021-11-11Java單線程ThreadLocal串值問(wèn)題解決方案
這篇文章主要介紹了Java單線程ThreadLocal串值問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Mybatis 一對(duì)多和多對(duì)一關(guān)聯(lián)查詢問(wèn)題
這篇文章主要介紹了Mybatis 一對(duì)多和多對(duì)一關(guān)聯(lián)查詢問(wèn)題,需要的朋友可以參考下2017-04-04mybatis定義sql語(yǔ)句標(biāo)簽之delete標(biāo)簽解析
這篇文章主要介紹了mybatis定義sql語(yǔ)句標(biāo)簽之delete標(biāo)簽解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03基于mybatis中數(shù)組傳遞注意事項(xiàng)
這篇文章主要介紹了mybatis中數(shù)組傳遞注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Java?web實(shí)現(xiàn)頭像上傳以及讀取顯示
這篇文章主要為大家詳細(xì)介紹了Java?web實(shí)現(xiàn)頭像上傳以及讀取顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06