如何制作不相等的GridLayout行

时间:2014-11-25 13:47:10

标签: java eclipse eclipse-plugin swt

我想让每个单元格成一个不同的长度.. 这是一张图片来帮助。

所以Policy单元格和右边的文本都没问题。但是,第1节和第2节我希望是50/50 ..不是20/80或现在的任何东西。我已经开始使用WindowsBuilder工具而不是手动执行此操作。这可能吗?

2 个答案:

答案 0 :(得分:1)

要使用SWT中的GridLayout以请求的方式布局控件,您必须每行的控件分组为自己的组合,如下所示:

shell.setLayout( new RowLayout( SWT.VERTICAL ) );
Composite composite1 = new Composite( shell, SWT.NONE );
composite1.setLayout( new GridLayout( 2, false ) );
createLabel( composite1, "2020" );
createLabel( composite1, "808080808080" );

Composite composite2 = new Composite( shell, SWT.NONE );
composite2.setLayout( new GridLayout( 2, false ) );
createLabel( composite2, "50505050" );
createLabel( composite2, "50505050" );

private static Label createLabel( Composite parent, String text ) {
  Label label = new Label( parent, SWT.NONE );
  label.setText( text );
  return label;
}

然而,对我来说,FormLayout似乎更适合解决给定的问题:

shell.setLayout( new FormLayout() );
FormData leftFormData = new FormData();
leftFormData.top = new FormAttachment( 0 );
leftFormData.left = new FormAttachment( 0 );
leftFormData.right = new FormAttachment( 20 );
Label leftLabel = createLabel( shell, "2020", leftFormData );
FormData rightFormData = new FormData();
rightFormData.top = new FormAttachment( 0 );
rightFormData.left = new FormAttachment( leftLabel );
rightFormData.right = new FormAttachment( 100 );
createLabel( shell, "808080808080", rightFormData );

private static Label createLabel( Composite parent, String text, Object layoutData ) {
  Label label = new Label( parent, SWT.NONE );
  label.setText( text );
  label.setLayoutData( layoutData );
  return label;
}

如果您发现formData和formAttachment代码过于冗长,您可以查看Slim Down SWT FormLayout Usage

有关SWT布局的深入讨论,我建议使用Understanding Layouts in SWT文章。

答案 1 :(得分:0)

编辑:错过您使用SWT GridLayout而不是Swing。我的解决方案适用于Swing,请记住这一点。

您可能想要使用GridBagLayout。它比GridLayout更复杂,但提供了更大的灵活性

查看有关如何使用它的一些文档here