`

SWT 中Button和Label重叠放置

 
阅读更多

写了一个小例子:

 

public class Snippet206 {

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);

		Image image1 = display.getSystemImage(SWT.ICON_QUESTION);

		Image image2 = new Image(display,image1.getImageData().scaledTo(16, 16));
		Color color = new Color(null, 213, 222, 246);

		shell.setLayout(new GridLayout(3, false));
		new Composite(shell, SWT.BORDER).setLayoutData(new GridData(GridData.FILL_BOTH));
		new Composite(shell, SWT.BORDER).setLayoutData(new GridData(GridData.FILL_BOTH));
		new Composite(shell, SWT.BORDER).setLayoutData(new GridData(GridData.FILL_BOTH));

		new Composite(shell, SWT.BORDER).setLayoutData(new GridData(GridData.FILL_BOTH));

		Composite comp = new Composite(shell, SWT.BORDER);
		comp.setLayoutData(new GridData(GridData.FILL_BOTH));
		/* ******************************** */
		comp.setLayout(new FormLayout());
		FormData formData;

		// fill action to left
		int buttonSpace = 5;
		{
			Composite comp1 = new Composite(comp, SWT.NONE);
			formData = new FormData ();
			formData.top = new FormAttachment (0, buttonSpace);
			formData.left = new FormAttachment (0, buttonSpace);
			formData.bottom = new FormAttachment (100, -buttonSpace);
			comp1.setLayoutData (formData);
			comp1.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));

			{
				RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
				rowLayout.spacing = 8;
				comp1.setLayout(rowLayout);

				Label label1 = new Label(comp1,SWT.NONE);
				label1.setImage(image2);
				Label label2 = new Label(comp1,SWT.NONE);
				label2.setImage(image2);
				Label label3 = new Label(comp1,SWT.NONE);
				label3.setImage(image2);
			}

			formData = new FormData ();
			formData.top = new FormAttachment (0, buttonSpace);
			formData.left = new FormAttachment (comp1, 0);
			formData.bottom = new FormAttachment (100, -buttonSpace);
			Label label2 = new Label(comp,SWT.SEPARATOR);
			label2.setLayoutData(formData);
		}
		// fill action to right
		{
			Composite comp1 = new Composite(comp, SWT.NONE);
			formData = new FormData ();
			formData.top = new FormAttachment (0, buttonSpace);
			formData.right = new FormAttachment (100, -buttonSpace);
			formData.bottom = new FormAttachment (100, -buttonSpace);
			comp1.setLayoutData (formData);
			comp1.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));

			{
				RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
				rowLayout.spacing = 8;
				comp1.setLayout(rowLayout);

				Label label1 = new Label(comp1,SWT.NONE);
				label1.setImage(image2);
				Label label2 = new Label(comp1,SWT.NONE);
				label2.setImage(image2);
				Label label3 = new Label(comp1,SWT.NONE);
				label3.setImage(image2);
			}

			formData = new FormData ();
			formData.top = new FormAttachment (0, buttonSpace);
			formData.right = new FormAttachment (comp1, 0);
			formData.bottom = new FormAttachment (100, -buttonSpace);
			Label label2 = new Label(comp,SWT.SEPARATOR);
			label2.setLayoutData(formData);
		}

		Button button = new Button(comp, SWT.PUSH);
		button.setImage(display.getSystemImage(SWT.ICON_QUESTION));
		button.setText("Some Action");

		formData = new FormData ();
		formData.top = new FormAttachment (0, 0);
		formData.left = new FormAttachment (0, 0);
		formData.right = new FormAttachment (100, 0);
		formData.bottom = new FormAttachment (100, 0);
		button.setLayoutData (formData);


		/* ******************************** */

		new Composite(shell, SWT.BORDER).setLayoutData(new GridData(GridData.FILL_BOTH));;
		new Composite(shell, SWT.BORDER).setLayoutData(new GridData(GridData.FILL_BOTH));;
		new Composite(shell, SWT.BORDER).setLayoutData(new GridData(GridData.FILL_BOTH));;
		new Composite(shell, SWT.BORDER).setLayoutData(new GridData(GridData.FILL_BOTH));;

		shell.setSize(800, 560);
		shell.open();
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		image2.dispose();
		color.dispose();
		display.dispose ();
	}
}

 

效果图:



 

 

  • 大小: 18 KB
分享到:
评论

相关推荐

    SWT中文教程.rar

    - **Button**: 包括复选框(CheckBox)、单选按钮(RadioButton)和普通按钮(Button),用于用户交互。 - **Composite**: 作为容器,可以包含其他组件,实现复杂的布局管理。 - **Control**: 所有SWT组件的基类...

    SWT JFace 按键、事件、监听

    本文将详细介绍SWT中的按键及其对应的常量表、`KeyEvent`事件和`VerifyEvent`事件的相关知识,并通过示例进行说明。 #### 按键与对应的常量表 SWT定义了一系列的常量来表示不同的按键,这些常量通常用于按键事件的...

    SWT插件中文教程SWT常用方法

    SWT提供了几种布局管理器,如FillLayout、GridLayout、FormLayout等,用于控制控件在窗口中的位置和大小。 `Swt常用控件中文教程 - E动中国.files`可能包含的是教程的辅助资源,如图片、样式表或者示例代码。这些...

    SWT 基本 窗口 组件

    java SWT 基本窗口 组件 开发

    SWT界面设计PDF

    1. **组件(Widgets)**:SWT中的每个可视元素都是一个组件,如Button、Label、Text等。 2. **壳体(Shell)**:壳体是SWT应用的基本窗口,所有控件都放置在壳体内。 3. **布局管理器(Layouts)**:布局管理器负责...

    swt中treeview和tableview实例

    swt中treeview和tableview实例,有助于理解这两个控件的使用。另外附带html页面。。可读性更强。

    如何在SWT中使用AWT、SWING组件

    在这个例子中,我们创建了一个`Frame`作为SWT和AWT之间的桥梁,然后在AWT `Frame`内添加了一个`Button`。 **在SWT中使用SWING组件** 对于SWING组件,我们需要使用`SWT.SWING`桥接类。下面是一个使用SWING组件的例子...

    SWT widgets基本教程

    Label sep1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.SHADOW_IN); // ... 更多标签示例 ... ``` ##### 4.2 文本框(Text) - **Text** 组件用于输入或显示单行或多行文本。 - 创建单行文本框...

    SWT开发教程1

    在SWT中,开发者可以使用各种组件来构建GUI,如Button、Text、Label、Shell、Composite、Tree、Table等。这些组件提供了丰富的事件处理机制,允许开发者响应用户的操作。例如,Button组件可以处理点击事件,Text组件...

    使Excel嵌入到SWT窗口中

    本例展示了如何使用SWT和OLE技术将Excel嵌入到SWT窗口中。同时,我们也讨论了Eclipse版本和jdk版本的选择、SWT的使用、OLE技术的使用、OleFrame和OleClientSite的使用、OLE对象的Id和OLE对象的动作等知识点。

    在SWING、SWT中嵌入浏览器

    在Java编程环境中,SWING和SWT(Standard Widget Toolkit)是两种常见的图形用户界面(GUI)库,用于创建桌面应用程序。然而,有时我们可能需要在这些应用中集成Web浏览功能,比如显示网页或者运行Web应用。这篇博客...

    SWT32位和64位库

    SWT设计的目标是提供一个更高效、更原生的GUI,与Java标准库中的Abstract Window Toolkit (AWT) 和Swing相比,SWT能够更好地利用操作系统提供的功能,从而实现更快的性能和更好的用户体验。 标题“SWT32位和64位库...

    SWT和JFace开发实例

    - 基本组件:SWT提供了丰富的UI组件,如Button、Label、Text、Shell、Composite等,它们对应着操作系统中的原生控件。 - 事件处理:SWT使用监听器模式来处理用户交互,如MouseListener、KeyListener和...

    swt教程 有关SWT怎么应用

    3. **配置构建路径**:在Eclipse项目中,需要将SWT库的JAR文件添加到构建路径中,以便编译和运行时能正确引用。 4. **编写代码**:创建一个新的Java类,例如HelloSWT.java,导入必要的SWT包,然后编写显示GUI组件的...

    swt-64.rar_64位swt_64的SWT_SWT64_swt.jar 64位_swt64位

    开发者在构建针对64位Windows的Java应用时,需要将这个jar文件添加到类路径中,以便能够使用SWT提供的各种组件和功能。 SWT提供了丰富的组件,包括按钮、文本框、滚动条、树形视图、表格、菜单等,这些组件都可以在...

    32位swt的jar包

    在开发SWT应用时,了解SWT的基本组件和布局管理器是很重要的,例如Button、Label、Text、Composite等控件,以及FillLayout、GridLayout、FormLayout等布局。此外,SWT还提供了事件处理机制,允许开发者响应用户的...

    swt - designer 学习实例

    SWT组件是构建GUI的基本元素,包括Button、Label、Text、Shell等,它们对应于常见的GUI控件。每个组件都有其特定的方法和属性,可以进行定制和交互。 布局管理在SWT中扮演着重要角色,它决定了组件在窗口中的位置和...

    JAVA swt 开发第二部分

    在Eclipse_Swt_Jface_核心应用系列教程中,这部分内容会帮助开发者更好地理解和使用这两者。 3. SWT组件: - 控件(Widgets):包括Button、Text、Label等基本元素,它们是用户界面的基本构建块。 - 布局...

    SWT中bean与控件的数据绑定

    2. **Bean和控件**:bean通常是指包含数据和行为的对象,而在SWT中,控件是指UI元素,如Text、Button等。数据绑定就是将bean的属性与控件的值进行映射。 3. **SWT的数据绑定框架**:介绍SWT中用于实现数据绑定的库...

    SWT 中嵌入Word 控件应用

    这篇博客"SWT中嵌入Word控件应用"可能就是讲述如何在Java SWT界面中集成Word编辑功能,使得用户可以直接在应用程序内部创建、编辑和查看Word文档。 SWT是Eclipse开源项目的一部分,它为Java开发者提供了一套用于...

Global site tag (gtag.js) - Google Analytics