如何从组中删除标签占用的默认空间?

时间:2013-08-29 08:35:09

标签: java swt

我想在一个复合和组中创建一个包含2个单选按钮的标签。但是在调整时我遇到了问题。它与标签没有正确对齐。该组有一个我不使用的文本或标题。由于该文本,它没有正确对齐。但对于复合材料,我得到了正确的行为,并且它正确地与w.r.t对齐以进行标记。那么有没有办法使用组复制相同的东西。下面是我使用的快照和代码。

代码:

shell.setLayout(new GridLayout(2,false));
GridData grid=new GridData();
Composite comp=new Composite(shell, SWT.None);

comp.setLayout(new GridLayout());
comp.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
Label label = new Label(comp, SWT.None);
label.setText("Invertframe:");

org.eclipse.swt.widgets.Group group=new org.eclipse.swt.widgets.Group(shell, SWT.None);
GridLayout layout = new GridLayout(2, true);

layout.horizontalSpacing = 0;
layout.verticalSpacing = 10;
layout.marginBottom = 0;
layout.marginHeight = 0;
layout.marginWidth = 0;

group.setLayout(layout);

group.setLayoutData(grid);
Button button =new  Button(group, SWT.RADIO);
button.setText("ON");
Button button2 =new  Button(group, SWT.RADIO);
button2.setText("OFF");
comp=new Composite(shell, SWT.None);

comp.setLayout(new GridLayout());
comp.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
label = new Label(comp, SWT.None);
label.setText("Invertframe:");

Composite composite2=new Composite(shell, SWT.BORDER);
layout = new GridLayout(2, true);
composite2.setLayout(layout);
layout.horizontalSpacing = 0;
layout.verticalSpacing = 10;
layout.marginBottom = 0;
layout.marginHeight = 0;
layout.marginWidth = 0;

button =new  Button(composite2, SWT.RADIO);
button.setText("ON");
button2 =new  Button(composite2, SWT.RADIO);
button2.setText("OFF");

enter image description here

2 个答案:

答案 0 :(得分:2)

您可能没有尝试过否定verticalIndent

protected void createContents()
{
    shell.setLayout(new GridLayout(2, false));
    GridData grid = new GridData();
    grid.verticalAlignment = SWT.FILL;
    grid.verticalIndent = -6;
    Composite comp = new Composite(shell, SWT.None);

    comp.setLayout(new GridLayout());
    comp.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
    Label label = new Label(comp, SWT.None);
    label.setText("Invertframe:");

    org.eclipse.swt.widgets.Group group = new org.eclipse.swt.widgets.Group(shell, SWT.None);
    GridLayout layout = new GridLayout(2, true);

    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 0;
    layout.marginBottom = 0;
    layout.marginHeight = 0;
    layout.marginWidth = 0;

    group.setLayout(layout);

    group.setLayoutData(grid);
    Button button = new Button(group, SWT.RADIO);
    GridData gd_button = new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1);
    gd_button.verticalIndent = -2;
    button.setLayoutData(gd_button);

    button.setText("ON");
    Button button2 = new Button(group, SWT.RADIO);
    GridData gd_button2 = new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1);
    gd_button2.verticalIndent = -2;
    button2.setLayoutData(gd_button2);
    button2.setText("OFF");


    comp = new Composite(shell, SWT.None);

    comp.setLayout(new GridLayout());
    comp.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
    label = new Label(comp, SWT.None);
    label.setText("Invertframe:");

    Composite composite2 = new Composite(shell, SWT.BORDER);
    layout = new GridLayout(2, true);
    composite2.setLayout(layout);
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 10;
    layout.marginBottom = 0;
    layout.marginHeight = 0;
    layout.marginWidth = 0;

    button = new Button(composite2, SWT.RADIO);
    button.setText("ON");
    button2 = new Button(composite2, SWT.RADIO);
    button2.setText("OFF");
}

aligned with Group

答案 1 :(得分:0)

嗯,你是对的。该行为是由Group的空标题字符串引起的。
我认为您无法获得所需的行为,但您可能想尝试将以下GridLayout应用于您的论坛:

final GridLayout grid = (GridLayout) layout;
grid.horizontalSpacing = grid.marginBottom = grid.marginHeight = grid.marginLeft = grid.marginRight = grid.marginTop = grid.marginWidth = grid.verticalSpacing = 0;