SWT布局问题 - 标签填充可能吗? [英] SWT layout problem - padding for labels possible?
问题描述
我在SWT GUI应用程序中使用GridLayout。我为每个网格单元定义了以下GridData。网格单元本身只是一个标签。
I am using GridLayout in my SWT GUI app. I have the following GridData defined for each grid cell. The grid cell itself is just a label.
GridData gridData = new GridData();
gridData.horizontalAlignment = GridData.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.heightHint = 25;
gridData.widthHint = 25;
gridData.verticalAlignment = GridData.VERTICAL_ALIGN_CENTER;
gridData.verticalIndent = 10;
我创建了这样的每个标签元素 -
And I create each lable element like this -
Label l = new Label(shell, SWT.BORDER);
l.setAlignment(SWT.CENTER);
l.setText("some text");
l.setLayoutData( gridData );
现在我的问题是尽管在标签本身上使用了verticalAlignment属性,verticalIndent属性和setAlignment,我我无法使文本相对于网格单元区域(25 x 25)垂直居中对齐。我想我错过了一些东西。如何在网格单元格中实现垂直居中对齐?
Now My problem is in spite of using verticalAlignment property, verticalIndent property and setAlignment on the label itself, I am not able to get the text align vertically centered with respect to the grid cell area ( 25 x 25 ). I think I am missing something. How can I achieve the vertically centered alignment in a grid cell ?
推荐答案
标签上没有setVerticalAlignment(),如果你使用LayoutData中的heightHint设置高度,该高度将成为控件的大小,并有效地阻止您将其设置在中间。解决方法是将Label放入具有所需大小的合成中,然后将控件置于中心位置。例如,
There is no setVerticalAlignment() on a Label and if you set the height, using the heightHint in the LayoutData, that becomes the size of the control and effectively stopping you from setting it in the middle. A workaround would be to put the Label into a composite with the size you want and then center the control. E.g.
Composite labelCell = new Composite(shell, SWT.BORDER);
GridData gridData = new GridData();
gridData.heightHint = 250;
gridData.widthHint = 250;
labelCell.setLayoutData(gridData);
labelCell.setLayout(new GridLayout());
Label l = new Label(labelCell , SWT.NONE);
l.setText("some text");
l.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
这篇关于SWT布局问题 - 标签填充可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!