Overview
When writing applications in the Standard Widget Toolkit (SWT), you may need to use layouts to give your windows a specific look. A layout controls the position and size of children in a Composite
. Layout classes are subclasses of the abstract class Layout
. SWT provides several standard layout classes, and you can write custom layout classes.
In SWT, positioning and sizing does not happen automatically. Applications can decide to size and place a Composite
's children initially, or in a resize listener. Another option is to specify a layout class to position and size the children. If children are not given a size, they will have zero size and they cannot be seen.
The diagram below illustrates a few general terms that are used when discussing layouts. The Composite
(in this case, a TabFolder
) has a location, clientArea and trim. The size of the Composite
is the size of the clientArea plus the size of the trim
. This Composite
has two children that are laid out side by side. A Layout
is managing the size and position of the children. This Layout
allows spacing
between the children, and a margin between the children and the edges of the Layout
. The size of the Layout
is the same as the size of theComposite
's clientArea.

The preferred size of a widget is the minimum size needed to show its content. In the case of a Composite
, the preferred size is the smallest rectangle that contains all of its children. If children have been positioned by the application, the Composite
computes its own preferred size based on the size and position of the children. If a Composite
is using a layout class to position its children, it asks the Layout
to compute the size of its clientArea
, and then it adds in the trim
to determine its preferred size.
Standard Layouts
The standard layout classes in the SWT library are:
-
FillLayout
lays out equal-sized widgets in a single row or column
-
RowLayout
lays out widgets in a row or rows, with fill, wrap, and spacing options
-
GridLayout
lays out widgets in a grid
-
FormLayout
lays out widgets by creating attachments for each of their sides
To use the standard layouts, you need to import the SWT layout package:
import org.eclipse.swt.layout.*;
Layouts are pluggable. To set a Composite
widget's layout, you use the widget's setLayout(Layout)
method. In the following code, a Shell
(a subclass of Composite
) is told to position its children using a RowLayout
:
Shell shell = new Shell();
shell.setLayout(new RowLayout());
A layout class may have a corresponding layout data class: a subclass of Object
that contains layout data for a specific child. By convention, layout data classes are identified by substituting "Data" for "Layout" in the class name. For example, the standard layout class RowLayout
has a layout data class called RowData
, the layout class GridLayout
uses a layout data class called GridData
, and the layout class FormLayout
has a layout data class called FormData
. A widget's layout data class is set as follows:
Button button = new Button(shell, SWT.PUSH);
button.setLayoutData(new RowData(50, 40));
Examples in this Document
Most of the snapshots in this document were taken by running variations on the following example code. We may change the type of layout, the options used, or the type or number of children.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class LayoutExample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
// Create the layout.
RowLayout layout = new RowLayout();
// Optionally set layout fields.
layout.wrap = true;
// Set the layout into the composite.
shell.setLayout(layout);
// Create the children of the composite.
new Button(shell, SWT.PUSH).setText("B1");
new Button(shell, SWT.PUSH).setText("Wide Button 2");
new Button(shell, SWT.PUSH).setText("Button 3");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
}
}
Running the above code results in the following:

If the user resizes the shell so that there is no longer room for Button 3 on the right, the RowLayout
wraps Button 3 to the next row, as follows:

Using layouts is closely tied with resize, as we shall see. Consequently, most of the examples in this document show what would happen if the Composite
becomes smaller or larger, in order to illustrate how the Layout
works.
FillLayout
FillLayout
is the simplest layout class. It lays out widgets in a single row or column, forcing them to be the same size. Initially, the widgets will all be as tall as the tallest widget, and as wide as the widest. FillLayout
does not wrap, and you cannot specify margins or spacing. You might use it to lay out buttons in a task bar or tool bar, or to stack checkboxes in aGroup
. FillLayout
can also be used when a Composite
only has one child. For example, if a Shell
has a single Group
child, FillLayout
will cause the Group
to completely fill the Shell
.
Here is the relevant portion of the example code. First we create a FillLayout
, then (if we want vertical) we set its type
field to SWT.VERTICAL
, and set it into the Composite
(aShell
). The Shell
has three push button children, "B1", "Wide Button 2", and "Button 3". Note that in a FillLayout
, children are always the same size, and they fill all available space.
FillLayout fillLayout = new FillLayout();
fillLayout.type = SWT.VERTICAL;
shell.setLayout(fillLayout);
new Button(shell, SWT.PUSH).setText("B1");
new Button(shell, SWT.PUSH).setText("Wide Button 2");
new Button(shell, SWT.PUSH).setText("Button 3");
The following table shows the differences between a horizontal and vertical FillLayout
, initially and after the parent has grown.
|
Initial
|
After resize
|
fillLayout.type = SWT.HORIZONTAL
(default)
|
 |
 |
fillLayout.type = SWT.VERTICAL |
 |
 |
RowLayout
RowLayout
is more commonly used than FillLayout
because of its ability to wrap, and because it provides configurable margins and spacing. RowLayout
has a number of configuration fields. In addition, the height and width of each widget in a RowLayout
can be specified by setting the widget's RowData
object using setLayoutData
.
RowLayoutConfiguration Fields
The type
field controls whether the RowLayout
lays out widgets in horizontal rows, or vertical columns. RowLayouts
are horizontal by default.
The wrap
field controls whether or not the RowLayout
will wrap widgets into the next row if there isn't enough space in the current row. RowLayouts
wrap by default.
If the pack
field is true, widgets in a RowLayout
will take their natural size ("natural size" varies by widget; the natural size for a label or push button, for example, is large enough to display its textual contents), and they will be aligned as far to the left as possible. If pack is false, widgets will fill the available space, similar to the widgets in a FillLayout
.RowLayouts
pack by default.
If the justify
field is true, widgets in a RowLayout
are spread across the available space from left to right. If the parent Composite
grows wider, the extra space is distributed evenly among the widgets. If both pack
and justify
are true, widgets take their natural size, and the extra space is placed between the widgets in order to keep them fully justified. By default, RowLayouts
do not justify.
The marginLeft
, marginTop
, marginRight
, marginBottom
and spacing
fields control the number of pixels between widgets (spacing
) and the number of pixels between a widget and the side of the parent Composite
(margin). By default, RowLayouts
leave 3 pixels for margins and spacing. The margin and spacing fields are shown in the following diagram.

RowLayout Examples
The following example code creates a RowLayout
, sets all of its fields to non-default values, and then sets it into a Shell
.
RowLayout rowLayout = new RowLayout();
rowLayout.wrap = false;
rowLayout.pack = false;
rowLayout.justify = true;
rowLayout.type = SWT.VERTICAL;
rowLayout.marginLeft = 5;
rowLayout.marginTop = 5;
rowLayout.marginRight = 5;
rowLayout.marginBottom = 5;
rowLayout.spacing = 0;
shell.setLayout(rowLayout);
If you are using the default field values, you only need one line of code:
shell.setLayout(new RowLayout());
The results of setting specific fields is shown below:
|
Initial
|
After resize
|
rowLayout.wrap = true;
rowLayout.pack = true;
rowLayout.justify = false;
rowLayout.type = SWT.HORIZONTAL;
(defaults)
|
 |
 and
|
wrap = false
(clips if not enough space)
|
 |
 |
pack = false
(all widgets are the same size)
|
 |
 |
justify = true
(widgets are spread across the available space)
|
 |
 |
type = SWT.VERTICAL
(widgets are arranged vertically in columns)
|
 |
 |
Using RowData Objects with RowLayout
Each widget controlled by a RowLayout
can have its initial width and height specified by setting its RowData
object. The following code uses RowData
objects to change the initial size of the Buttons
in a Shell
.
package org.eclipse.articles.layouts.samples;
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.Shell;
public class RowDataExample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("Button 1");
button1.setLayoutData(new RowData(50, 40));
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("Button 2");
button2.setLayoutData(new RowData(50, 30));
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("Button 3");
button3.setLayoutData(new RowData(50, 20));
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
Here is what you see when you run this code.

GridLayout
With a GridLayout
, the widget children of a Composite
are laid out in a grid. GridLayout
has a number of configuration fields, and—like RowLayout
—the widgets it lays out can have an associated layout data object, called GridData
. The power of GridLayout
lies in the ability to configure GridData
for each widget controlled by the GridLayout
.
GridLayout Configuration Fields
The numColumns
field is the most important field in a GridLayout
, and it is usually the first field an application will set. Widgets are laid out in columns from left to right, and a new row is created when numColumns
+ 1 widgets are added to the Composite
. The default is to have only 1 column. The following code creates a Shell
with five Button
children of various widths, managed by a GridLayout
. The table below shows the grid when numColumns
is set to one, two, or three.
Display display = new Display();
Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
shell.setLayout(gridLayout);
new Button(shell, SWT.PUSH).setText("B1");
new Button(shell, SWT.PUSH).setText("Wide Button 2");
new Button(shell, SWT.PUSH).setText("Button 3");
new Button(shell, SWT.PUSH).setText("B4");
new Button(shell, SWT.PUSH).setText("Button 5");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
numColumns = 1
|
numColumns = 2
|
numColumns = 3
|
 |
 |
 |
The makeColumnsEqualWidth
field forces the columns to be the same width. The default is false
. If we change the example above to have three columns of equal width, this is what we would get (note that in the absence of further instruction, widgets are left-justified in their columns).

The marginWidth
, marginHeight
, horizontalSpacing
, and verticalSpacing
fields in a GridLayout
are similar to those in a RowLayout
. The difference is that the left and right margins are grouped into marginWidth
, and the top and bottom margins are grouped into marginHeight
. Also, in a GridLayout
you can specify horizontalSpacing
and verticalSpacing
independently, whereas in a RowLayout
, spacing
applies to horizontal or vertical depending on the type of the RowLayout
.
GridData Object Fields
GridData
is the layout data object associated with font-style: normal; font-weight: normal; f
分享到:
相关推荐
首先,我们来看《Understanding Layouts in SWT.doc》这份文档。SWT(Standard Widget Toolkit)是Eclipse中用于创建用户界面的基础库,它提供了各种控件和布局管理器。布局管理器是SWT中用来控制控件在容器中排列...
1. **理解布局(Understanding Layouts)** - **资源地址**:[http://www.eclipse.org/articles/article.php?file=Article-Understanding-Layouts/index.html]...
内容概要:本文深入剖析了安川MP7系列工业控制系统的关键源码,重点介绍了运动轨迹规划、通信协议处理以及故障处理机制等方面的技术细节。通过对实际代码片段的解读,揭示了该系统在硬件寄存器直接访问、特殊功能码处理等方面的独特之处。同时,文中还分享了一些基于实践经验得出的重要参数设置及其背后的故事,如特定摩擦补偿系数的选择原因等。 适合人群:从事工业自动化领域的工程师和技术人员,尤其是对安川产品有一定了解并希望深入了解其内部工作机制的专业人士。 使用场景及目标:帮助读者掌握安川MP7系列控制器的工作原理,提高对类似系统的维护能力和故障排查效率。对于想要进一步研究或二次开发该系统的开发者来说,也能提供宝贵的参考资料。 其他说明:文章不仅限于理论讲解,还包括了许多来自一线的实际案例和经验教训,使读者能够更好地理解和应用所学知识。
自动化测试与脚本开发_Python3_pynput_键盘鼠标操作录制执行代码生成工具_用于自动化测试_脚本录制_重复操作模拟_宏命令生成_提高工作效率_支持GUI界面_跨平台兼容_
嵌入式八股文面试题库资料知识宝典-深入分析Windows和Linux动态库应用异同.zip
嵌入式八股文面试题库资料知识宝典-C语言总结.zip
内容概要:本文详细探讨了风储直流微电网中母线电压控制的关键技术。首先介绍了风储直流微电网的背景和发展现状,强调了母线电压控制的重要性。接着阐述了永磁风机储能并网技术,解释了永磁风机如何通过直接驱动发电机将风能转化为电能,并确保与电网的同步性和稳定性。然后深入讨论了双闭环控制MPPT技术,这是一种通过内外两个闭环控制系统来实现实时调整发电机运行参数的技术,确保风机始终处于最大功率点附近。最后,文章探讨了储能控制母线电压平衡的方法,即通过储能系统的充放电操作来维持母线电压的稳定。结论部分指出,通过这些技术的有机结合,可以实现对风储直流微电网的有效管理和优化控制。 适合人群:从事新能源技术研发的专业人士、电气工程研究人员、风电系统工程师。 使用场景及目标:适用于希望深入了解风储直流微电网母线电压控制策略的研究人员和技术人员,旨在帮助他们掌握最新的控制技术和方法,以提高系统的稳定性和效率。 其他说明:文章还对未来风储直流微电网的发展进行了展望,指出了智能化和自动化的趋势,以及储能技术的进步对系统性能的影响。
嵌入式八股文面试题库资料知识宝典-C++object-oriented.zip
内容概要:文章详细介绍了HarmonyOS的目录结构及其重要性,从整体框架到核心目录的具体功能进行了全面剖析。HarmonyOS凭借其分布式架构和跨设备协同能力迅速崛起,成为全球操作系统领域的重要力量。文章首先概述了HarmonyOS的背景和发展现状,强调了目录结构对开发的重要性。接着,具体介绍了根目录文件、AppScope、entry和oh_modules等核心目录的功能和作用。例如,AppScope作为全局资源配置中心,存放应用级的配置文件和公共资源;entry目录是应用的核心入口,负责源代码和界面开发。此外,文章还对比了HarmonyOS与Android、iOS目录结构的异同,突出了HarmonyOS的独特优势。最后,通过旅游应用和电商应用的实际案例,展示了HarmonyOS目录结构在资源管理和代码组织方面的应用效果。; 适合人群:具备一定编程基础,尤其是对移动操作系统开发感兴趣的开发者,包括初学者和有一定经验的研发人员。; 使用场景及目标:①帮助开发者快速理解HarmonyOS的目录结构,提高开发效率;②为跨设备应用开发提供理论和技术支持;③通过实际案例学习资源管理和代码组织的最佳实践。; 其他说明:HarmonyOS的目录结构设计简洁明了,模块职责划分明确,有助于开发者更好地管理和组织代码和资源。随着万物互联时代的到来,HarmonyOS有望在开发便利性和生态建设方面取得更大进展,吸引更多开发者加入其生态系统。
内容概要:本文详细介绍了飞轮储能充放电控制的Simulink仿真模型,重点在于采用永磁同步电机的矢量控制和dq轴解耦控制策略。充电时,外环控制转速,内环控制dq轴电流;放电时,外环控制直流母线电压,内环同样控制dq轴电流。文中还讨论了硬件与软件环境的选择,以及仿真模型的调试与运行情况,最终得出该模型具有良好的跟随性能和波形完美度。 适用人群:从事电力电子系统、储能技术和Simulink仿真的研究人员和技术人员。 使用场景及目标:适用于需要对飞轮储能系统进行深入研究和仿真的场合,旨在提高充放电效率和稳定性,满足不同应用场景的需求。 其他说明:该仿真模型已调试完成,可以直接用于进一步的研究和实际应用,为未来的飞轮储能技术研发提供了有价值的参考。
嵌入式八股文面试题库资料知识宝典-北京瑞德方科技.zip
嵌入式八股文面试题库资料知识宝典-同方万维硬件测试工程师.zip
1_15套python PDF格式.zip
内容概要:本文详细介绍了三相三电平整流器的仿真过程及其性能分析。文中首先概述了三相三电平整流器的基本概念及其在电力系统中的重要作用,接着重点探讨了电压电流双闭环控制方式的工作原理和优势,以及SPWM调制技术的具体应用。通过仿真文件展示了整流器在不同条件下的响应情况,验证了这两种技术的有效性和优越性。最后,作者表达了对未来实际应用的期望。 适合人群:从事电力电子研究的技术人员、高校相关专业师生、对电力控制系统感兴趣的工程爱好者。 使用场景及目标:适用于希望深入了解三相三电平整流器工作原理和技术细节的研究人员;目标是在理论基础上掌握电压电流双闭环控制和SPWM调制的实际应用方法。 其他说明:本文提供的仅为仿真文件,未涉及实物实验数据。
嵌入式八股文面试题库资料知识宝典-恒光科技.zip
嵌入式八股文面试题库资料知识宝典-北京天华威视科技有限公司面试题.zip
嵌入式八股文面试题库资料知识宝典-微软研究院笔试题目的答案.zip
Arduino UART实验例程,开发板:正点原子EPS32S3,本人主页有详细实验说明可供参考。
嵌入式八股文面试题库资料知识宝典-朝歌数码.zip
嵌入式八股文面试题库资料知识宝典-Cortex系列.zip