SWT问题:使用GridData对标签进行对齐和填充时出现问题。

huangapple go评论73阅读模式
英文:

SWT Issue in Aligning + Filling a Label with GridData

问题

所以,我尝试制作了一个小游戏,在游戏中你必须执行标签显示的动作。

我的问题是,我无法将标签与列的中心对齐,并且**同时**填充整个列的空间以便我可以使用背景颜色。

例如:

```java
Shell shell = new Shell(display);

GridLayout layout = new GridLayout();
Font font = new Font(display, "Arial", 30, SWT.NONE);

layout.numColumns = 2;
layout.marginHeight = 0;
layout.marginWidth = 0;

shell.setText("ReactionGame 2.0");
shell.setMaximized(true);
shell.setLayout(layout);
shell.setBackground(display.getSystemColor(SWT.COLOR_BLACK));

GridData data1 = new GridData(SWT.CENTER, SWT.CENTER, true, true);

data1.verticalAlignment = GridData.FILL;
data1.horizontalAlignment = GridData.FILL;

Label lifelabel = new Label(shell, SWT.NONE);

lifes(lifelabel);
lifelabel.setForeground(display.getSystemColor(SWT.COLOR_RED));
lifelabel.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
lifelabel.setFont(font);

lifelabel.setLayoutData(data1);

Label scorelabel = new Label(shell, SWT.NONE);

scorecount(scorelabel);
scorelabel.setForeground(display.getSystemColor(SWT.COLOR_RED));
scorelabel.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
scorelabel.setFont(font);

scorelabel.setLayoutData(data1);

Label gamelabel = new Label(shell, SWT.NONE);

gamelabel.setText("");
gamelabel.setForeground(display.getSystemColor(SWT.COLOR_GREEN));
gamelabel.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
gamelabel.setFont(font);

gamelabel.setLayoutData(data1);

这段代码会产生如下设计:

我的问题是,我如何使标签的文本居中对齐?


<details>
<summary>英文:</summary>

So, I tried to make a little game where you have to do actions that are displayed by a label.

My problem is that I can&#39;t align the label in the center of a column **AND** fill the whole space of the column with the label so I can use the background cColor.

For example:

Shell shell = new Shell(display);

GridLayout layout = new GridLayout();
Font font = new Font(display, "Arial", 30, SWT.NONE);

layout.numColumns = 2;
layout.marginHeight = 0;
layout.marginWidth = 0;

shell.setText("ReactionGame 2.0");
shell.setMaximized(true);
shell.setLayout(layout);
shell.setBackground(display.getSystemColor(SWT.COLOR_BLACK));

GridData data1 = new GridData(SWT.CENTER, SWT.CENTER, true, true);

data1.verticalAlignment = GridData.FILL;
data1.horizontalAlignment = GridData.FILL;

Label lifelabel = new Label(shell, SWT.NONE);

lifes(lifelabel);
lifelabel.setForeground(display.getSystemColor(SWT.COLOR_RED));
lifelabel.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
lifelabel.setFont(font);

lifelabel.setLayoutData(data1);

Label scorelabel = new Label(shell, SWT.NONE);

scorecount(scorelabel);
scorelabel.setForeground(display.getSystemColor(SWT.COLOR_RED));
scorelabel.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
scorelabel.setFont(font);

scorelabel.setLayoutData(data1);

	Label gamelabel = new Label(shell, SWT.NONE);

gamelabel.setText("");
gamelabel.setForeground(display.getSystemColor(SWT.COLOR_GREEN));
gamelabel.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
gamelabel.setFont(font);

	gamelabel.setLayoutData(data1);

[This code gives me a design of this:][1]


  [1]: https://i.stack.imgur.com/HBMeF.png

My question now is how can I align the text of the label in the center?

</details>


# 答案1
**得分**: 2

您正在多个控件上重用同一个`GridData`(`data1`),这是不允许的。`GridData`实例用于保存有关各个控件的布局信息,您必须为每个控件使用一个新实例。

因此,请使用:

```java
lifelabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));

scorelabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));

gamelabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));

要创建第三个区域,您需要使用一个额外的带有标签居中且设置了背景颜色的Composite

Composite gameComp = new Composite(shell, SWT.NONE);

gameComp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

gameComp.setBackground(display.getSystemColor(SWT.COLOR_RED));

GridLayout gameLayout = new GridLayout();
gameLayout.marginHeight = 0;
gameLayout.marginWidth = 0;
gameComp.setLayout(gameLayout);

Label gamelabel = new Label(gameComp, SWT.NONE);

gamelabel.setText("game");
gamelabel.setForeground(display.getSystemColor(SWT.COLOR_GREEN));
gamelabel.setBackground(display.getSystemColor(SWT.COLOR_RED));
gamelabel.setFont(font);

gamelabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
英文:

You are reusing the same GridData (data1) for multiple controls - this is not allowed. The GridData instance is used to save layout information about individual controls, you must use a new instance for each control.

So use:

lifelabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));

scorelabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));

gamelabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));

To make the third area you will need to use an extra Composite with the label centred in that and a background colour set:

Composite gameComp = new Composite(shell, SWT.NONE);

gameComp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

gameComp.setBackground(display.getSystemColor(SWT.COLOR_RED));

GridLayout gameLayout = new GridLayout();
gameLayout.marginHeight = 0;
gameLayout.marginWidth = 0;
gameComp.setLayout(gameLayout);

Label gamelabel = new Label(gameComp, SWT.NONE);

gamelabel.setText(&quot;game&quot;);
gamelabel.setForeground(display.getSystemColor(SWT.COLOR_GREEN));
gamelabel.setBackground(display.getSystemColor(SWT.COLOR_RED));
gamelabel.setFont(font);

gamelabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));

huangapple
  • 本文由 发表于 2020年9月9日 14:23:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63805842.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定