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:表示子組件是否為保持原有大?。╰rue為保持原有大?。?。
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)文章
SpringBoot favicon Chrome設(shè)置問(wèn)題解決方案
在本篇文章里小編給大家分享的是關(guān)于SpringBoot favicon Chrome設(shè)置問(wèn)題實(shí)例內(nèi)容,小的朋友們可以參考學(xué)習(xí)下。2020-02-02
Java注解詳解及實(shí)現(xiàn)自定義注解的方法
這篇文章主要介紹了Java注解詳解及實(shí)現(xiàn)自定義注解的方法,本文給大家介紹了jdk中預(yù)定義的一些注解及自定義注解的相關(guān)知識(shí),需要的朋友可以參考下2022-06-06
Java中的信息摘要算法MessageDigest類用法詳解
這篇文章主要介紹了Java中的信息摘要算法MessageDigest類用法詳解,java.security.MessageDigest類為應(yīng)用程序提供信息摘要算法的功能,如MD5或SHA-1或SHA-256算法,信息摘要是安全的單向哈希函數(shù),它接收任意大小的數(shù)據(jù),并輸出固定長(zhǎng)度的哈希值,需要的朋友可以參考下2024-01-01
Java實(shí)現(xiàn)curl調(diào)用帶參數(shù)接口方法
本文主要介紹了Java實(shí)現(xiàn)curl調(diào)用帶參數(shù)接口方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04
SpringBoot使用Mybatis-Generator配置過(guò)程詳解
這篇文章主要介紹了SpringBoot使用Mybatis-Generator配置過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
springboot+mybatis-plus實(shí)現(xiàn)內(nèi)置的CRUD使用詳解
這篇文章主要介紹了springboot+mybatis-plus實(shí)現(xiàn)內(nèi)置的CRUD使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Java Redis分布式鎖的正確實(shí)現(xiàn)方式詳解
這篇文章主要介紹了Java Redis分布式鎖的正確實(shí)現(xiàn)方式詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09

