SWT在GridData中对齐+填充标签的问题

问题描述

因此,我试图做一个小游戏,您必须执行标签所显示的操作。

我的问题是我无法将标签对齐到列的中心并且并且用标签填充了列的整个空间,所以我可以使用背景cColor。

例如:

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:

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

解决方法

您要对多个控件重复使用相同的GridDatadata1)-不允许这样做。 GridData实例用于保存有关单个控件的布局信息,您必须为每个控件使用一个新实例。

因此使用:

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

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

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

要创建第三个区域,您将需要使用额外的Composite,标签以该标签为中心,并设置背景颜色:

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

gameComp.setLayoutData(new GridData(SWT.FILL,SWT.FILL,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,true));

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...