Java 使用 SWT gridlayout 和 griddata 学习

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13895372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:21:32  来源:igfitidea点击:

Java learn using SWT gridlayout and griddata

javagridswtalignmentgrid-layout

提问by Reteras Remus

I understand and can use FormLayout, FormData, FormAttachment but I can't understand how GridLayout, GridData is working. I want to learn using GridLayout and GridData because it's more like a table, it has a structure and doesn't depend on other widgets.

我理解并且可以使用 FormLayout、FormData、FormAttachment,但我无法理解 GridLayout、GridData 是如何工作的。我想学习使用 GridLayout 和 GridData 因为它更像是一个表格,它有一个结构并且不依赖于其他小部件。

I was working as a web developer (front-end, back-end) and I got lost in Java "Grid" structure. How am I supposed to align, move widgets within a cell (horizontal/vertical Aling, hor./vert. Indent)? Like in HTML/CSS: margin, padding, etc. Ex: move a block from left by 100px. (margin-left: 100px), but in Java?

我是一名 Web 开发人员(前端、后端),我迷失在 Java“网格”结构中。我应该如何对齐、移动单元格内的小部件(水平/垂直 Aling、hor./vert. Indent)?就像在 HTML/CSS 中一样:边距、填充等。例如:从左边移动一个块 100 像素。(左边距:100px),但在 Java 中?

When I was working as a web developer, I created a page (here in Java it's view), I know how to organize parents and blocks. Can I compare a Composite to a div, like a block element like in HTML/CSS ?

当我作为 Web 开发人员工作时,我创建了一个页面(这里是 Java 视图),我知道如何组织父级和块。我可以将 Composite 与 div 进行比较,就像 HTML/CSS 中的块元素一样吗?

I need to create the following app:

我需要创建以下应用程序:

enter image description here

在此处输入图片说明

Am I need to use 4 composites?

我需要使用 4 个复合材料吗?

回答by Baz

The following article should shed some light on GridLayoutfor you:

以下文章应该GridLayout为您提供一些启示:

Understanding Layouts in SWT

了解 SWT 中的布局

To achieve something like the form you have there, you would need something like this:

要实现类似于您在那里的形式的东西,您需要这样的东西:

shell.setLayout(new GridLayout(3, false));

Label title = new Label(shell, SWT.NONE);
title.setText("My first text editor");
GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
data.horizontalSpan = 3;
title.setLayoutData(data);

Label select = new Label(shell, SWT.NONE);
select.setText("Select a file:");
data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
select.setLayoutData(data);

Text text = new Text(shell, SWT.BORDER);
data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
text.setLayoutData(data);

Button button = new Button(shell, SWT.PUSH);
button.setText("Browse...");
data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
button.setLayoutData(data);

List result = new List(shell, SWT.BORDER);
data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.horizontalSpan = 3;
result.setLayoutData(data);    

The GridDataus used to define the behavior of the component within the layout. You can define vertical/horizontal alignment, margins and so on. horizontalSpanis used to tell the layout how many columns the widget will cover.

GridData我们使用的定义布局中组件的行为。您可以定义垂直/水平对齐方式、边距等。horizontalSpan用于告诉布局小部件将覆盖多少列。