SWT- setSize和setBounds在标签上不起作用

时间:2017-12-30 08:28:22

标签: java swt

我有一个标签。有时,它中的文本溢出并且不会显示在标签中。所以,我想改变标签的高度,也希望当它有溢出时,它会显示在下面的行中。我怎么能这样做?

composite = (Composite) super.createDialogArea(parent);
composite.setLayout(new GridLayout());
GridData data = new GridData(GridData.FILL_HORIZONTAL);    
Group grpModelProperties = new Group(composite, SWT.SHADOW_IN);
grpModelProperties.setLayout(new GridLayout(2, false));
grpModelProperties.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
Label l2 = new Label(grpModelProperties, SWT.NULL);
new Label(grpModelProperties, SWT.NONE);
l2.setLayoutData(data);
l2.setText("Text: " + Text);    
//l2.setBounds(0, 0, 470, 200);   
//l2.setSize( 470, 400 );

2 个答案:

答案 0 :(得分:0)

您可以将样式SWT.WRAP传递给标签构造函数,以指定如果没有足够的水平空间,文本应自动换行新行:

Label l2 = new Label(grpModelProperties, SWT.WRAP);

答案 1 :(得分:0)

要实现这一目标,需要进行一些更改。首先,您需要使用SWT.WRAP构造函数中的Label样式位。在你的情况下:

Label l2 = new Label(grpModelProperties, SWT.WRAP);

(作为旁注,如果您不想要样式集,则应使用SWT.NONE,而不是SWT.NULL

此外,您需要在顶级元素的布局数据上设置widthHint,否则您的对话框将展开以适应文本的整个宽度。在您的代码中:

composite.setLayout(new GridLayout());
GridData compositeGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
compositeGridData.widthHint = 200;
composite.setLayoutData(compositeGridData);

稍微清理一下,但你的代码最终应该是这样的:

@Override
protected Control createDialogArea(Composite parent) {
    composite = (Composite) super.createDialogArea(parent);
    composite.setLayout(new GridLayout());
    GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
    data.widthHint = 200;
    composite.setLayoutData(data);

    Group groupModelProperties = new Group(composite, SWT.SHADOW_IN);
    groupModelProperties.setLayout(new GridLayout(2, false));
    groupModelProperties.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

    Label label = new Label(groupModelProperties, SWT.WRAP);
    label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    label.setText("Text: The quick brown fox jumps over the lazy dog. This is a really long line of text that should wrap into a new line.");

    return composite;
}

enter image description here