-
Notifications
You must be signed in to change notification settings - Fork 5
Label Tutorial
标签(Label)一般情况下,就是用来显示一段文字或是图片的控件。在SWT中Label的实现类是org.eclipse.swt.widgets.Label,但是这个Label类,不但可以构造普通的标签,也可以构造分割线(Separator),关键取决于你所用的Style。
Label textLabel= new Label(shell, SWT.NONE);
textLabel.setText("Hello, I am a Label.");
Label imageLabel = new Label(shell, SWT.BORDER);
imageLabel.setImage(display.getSystemImage(SWT.ICON_QUESTION));

Tips:
一个Label,不能同时显示文本和图片,只能二选一。SWT.BORDER, 是指给Label定义了一个边框(Border)。
new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
new Label(shell, SWT.SEPARATOR | SWT.VERTICAL);

很简单吧?没错,两点:
-
SWT.SEPARATOR; -
SWT.HORIZONTAL或SWT.VERTICAL,顾名思义,分割线的方向。
Label提供了三种对齐的方式:左对齐(SWT.LEFT),居中对齐(SWT.CENTER)和右对齐(SWT.RIGHT),默认为左对齐。
设置方法有两种:
-
在创建的时候,加到style中,比如:
Label label = new Label(shell, SWT.CENTER);
-
通过调用setAlignment()方法。
Label label = new Label(shell, SWT.NONE); label.setAlignment(SWT.CENTER);
来个完整的例子:
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Alignment Tutorial");
shell.setSize(300, 200);
shell.setLayout(new GridLayout(3, true));
Label leftLabel = new Label(shell, SWT.LEFT|SWT.BORDER);
leftLabel.setText("Left");
leftLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL));
Label centerLabel = new Label(shell, SWT.CENTER | SWT.BORDER);
centerLabel.setText("Center");
centerLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL));
Label rightLabel = new Label(shell, SWT.BORDER);
rightLabel.setText("Right");
rightLabel.setAlignment(SWT.RIGHT);
rightLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL));
Label imageLeftLabel = new Label(shell, SWT.BORDER);
imageLeftLabel.setImage(display.getSystemImage(SWT.ICON_QUESTION));
imageLeftLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL));
Label imageCenterLabel = new Label(shell, SWT.BORDER | SWT.CENTER);
imageCenterLabel.setImage(display.getSystemImage(SWT.ICON_QUESTION));
imageCenterLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL));
Label imageRightLabel = new Label(shell, SWT.BORDER | SWT.RIGHT);
imageRightLabel.setImage(display.getSystemImage(SWT.ICON_QUESTION));
imageRightLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL));
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
上图:

遇到比较长分文本,而标签的宽度又不够时怎么办?别着急,可以试试自动换行(SWT.WRAP)。
请看下面的例子:
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Label Wrap Tutorial");
shell.setSize(300, 200);
shell.setLayout(new GridLayout(2, true));
Label noWrapLabel = new Label(shell, SWT.BORDER);
noWrapLabel.setText("This is a long Text on label, but no WRAP style set.");
noWrapLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
| GridData.GRAB_HORIZONTAL));
Label wrapLabel = new Label(shell, SWT.WRAP | SWT.BORDER);
wrapLabel.setText("This is a long Text on label, and the WRAP style is set.");
wrapLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
| GridData.GRAB_HORIZONTAL));
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
如图:

一目了然吧?
参考资料:
- Label snippets
- 如果想了解更多的关于设置颜色,字体等其它属性的相关内容,请移步至控件的通用设置。
- 如果想了解更多的关于
Layout和LayoutData的相关内容,请移步至布局管理器。 - SWT Example: ControlExample
- Sample code and further information
上一篇:Widgets Tutorial 下一篇:Button Tutorial
Soyatec Eclipse Plug-in and RCP Tutorials, wrote by Jin Liu (jin.liu@soyatec.com).
Eclipse Plug-in Development Tutorial
-
Standard Widget Toolkit Tutorial
-
Widgets Tutorial
- Label Tutorial
- Button Tutorial
- Text Tutorial
- Combo Tutorial
- List Tutorial
- Scale Tutorial
- Slider Tutorial
- Spinner Tutorial
- DateTime Tutorial
- Table Tutorial
- Tree Tutorial
- ToolBar Tutorial
- CoolBar Tutorial
- ProgressBar Tutorial
- ScrollBar Tutorial
- TaskBar Tutorial
- ExpandBar Tutorial
- Menu Tutorial
- Sash Tutorial
- ToolTip Tutorial
- Tray Tutorial
- Link Tutorial
- Browser Tutorial
- Dialog Tutorial
- Common Properties Tutorial
- Components Tutorial
- Shell Tutorial
- Display Tutorial
- SWT Custom Widgets Tutorial
- Drag and Drop Tutorial
- Layouts Tutorial
- Resource Management Tutorial
-
Widgets Tutorial
-
[JFace Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/JFace-Tutorial)
- [JFace Viewers Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/JFace-Viewers-Tutorial)
- [TableViewer Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/TableViewer-Tutorial)
- [TreeViewer Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/TreeViewer-Tutorial)
- [ListViewer Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/ListViewer-Tutorial)
- [ComboViewer Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/ComboViewer-Tutorial)
- [JFace Dialogs Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/JFace-Dialogs-Tutorial)
- [JFace DataBinding Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/JFace-DataBinding-Tutorial)
- [JFace Viewers Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/JFace-Viewers-Tutorial)
-
[Plug-in Development Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Plug-in-Development-Tutorial)
- [Views Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Views-Tutorial)
- [Editors Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Editors-Tutorial)
- [Action and Menus Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Action-and-Menus-Tutorial)
- [Preferences Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Preferences-Tutorial)
- [ExtensionPoint Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/ExtensionPoint-Tutorial)
- [Publish Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Publish-Tutorial)
- [Feature Project Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Feature-Project-Tutorial)
- [UpdateSite Project Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/UpdateSite-Project-Tutorial)
- [Fragment Project Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Fragment-Project-Tutorial)
-
[Eclipse 3.x RCP Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Eclipse-3.x-RCP-Tutorial)
- [Branding Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Branding-Tutorial)
- [Publish RCP Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/Publish-RCP-Tutorial)
-
[Graphical Editing Framework Tutorial] (https://github.com/ecsoya/eclipse.tutorial/wiki/GEF-Tutorial)