JAVA从入门到精通:SWT(JFace)体验之RowLayout布局
Vivian 2018-06-14 来源 : 阅读 785 评论 0

摘要:本文主要向大家介绍了JAVA从入门到精通的SWT(JFace)体验之RowLayout布局,通过具体的代码向大家展示,希望对大家学习JAVA从入门到精通有所帮助。

本文主要向大家介绍了JAVA从入门到精通的SWT(JFace)体验之RowLayout布局,通过具体的代码向大家展示,希望对大家学习JAVA从入门到精通有所帮助。

RowLayout布局 

相对于FillLayout来说,RowLayout比较灵活,功能也比较强。用户可以设置布局中子元素的大小、边距、换行及间距等属性。

RowLayout的风格

RowLayout中可以以相关的属性设定布局的风格,用户可以通过“RowLayout.属性”的方式设置RowLayout的布局风格,RowLayout中常用的属性如下。
Wrap:表示子组件是否可以换行(true为可换行)。
Pack:表示子组件是否为保持原有大小(true为保持原有大小)。
Justify:表示子组件是否根据父组件信息做调整。
MarginLeft:表示当前组件距离父组件左边距的像素点个数。
MarginTop:表示当前组件距离父组件上边距的像素点个数。
MarginRight:表示当前组件距离父组件右边距的像素点个数。
MarginBottom:表示当前组件距离父组件下边距的像素点个数。
Spacing:表示子组件之间的间距像素点个数。

另外,RowLayout可以通过RowData设置每个子组件的大小,例如“button.setLayoutData (new RowData(60, 60))”将设置button的大小为(60,60)。
测试代码: 

复制代码代码如下:

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(); 
} 
}


再看看下面这个动态的(结合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(); 
} 
}

希望对JAVA有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言JAVA频道!

本文由 @Vivian 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程