我可以组合SWT GridLayout和FillLayout

我有一个RCP / SWT应用程序,我正在尝试用现有的复合材料构建一个视图。 一个是FillLayout复合,另一个是GridLayout。

我想最终得到一个视图,其中GridLayout复合材料排列在FillLayout复合材料的左侧(想想垂直横幅),大约是整个视图宽度的10%,现有的FillLayout复合材料包含其他90%。

我不确定在SWT中是否可以组合布局,但我想的是像GridLayout这样的两列。 第一列包含GridLayout小部件,第二列包含FillLayout组合。 这可以在SWT中完成吗? 如果是这样,这看起来像代码?

谢谢-

开始将FormLayout设置为外部合成。 在其中放置另外两个复合材料,设置FormData信息以便根据需要定位它们。 然后设置这两个复合的布局(网格和填充,如你所说)。

这是一些开始的代码。 在显示它产生的东西之后有一个图像。 您还可以查看Eclipse的SWT Layouts视图。

 Shell shell = new Shell(); FillLayout fillLayout = new FillLayout(); fillLayout.marginHeight = 5; fillLayout.marginWidth = 5; shell.setLayout( fillLayout ); Composite outer = new Composite( shell, SWT.BORDER ); outer.setBackground( new Color( null, 207, 255, 206 ) ); // Green FormLayout formLayout = new FormLayout(); formLayout.marginHeight = 5; formLayout.marginWidth = 5; formLayout.spacing = 5; outer.setLayout( formLayout ); Composite innerLeft = new Composite( outer, SWT.BORDER ); innerLeft.setLayout( new GridLayout() ); innerLeft.setBackground( new Color( null, 232, 223, 255 ) ); // Blue FormData fData = new FormData(); fData.top = new FormAttachment( 0 ); fData.left = new FormAttachment( 0 ); fData.right = new FormAttachment( 10 ); // Locks on 10% of the view fData.bottom = new FormAttachment( 100 ); innerLeft.setLayoutData( fData ); Composite innerRight = new Composite( outer, SWT.BORDER ); innerRight.setLayout( fillLayout ); innerRight.setBackground( new Color( null, 255, 235, 223 ) ); // Orange fData = new FormData(); fData.top = new FormAttachment( 0 ); fData.left = new FormAttachment( innerLeft ); fData.right = new FormAttachment( 100 ); fData.bottom = new FormAttachment( 100 ); innerRight.setLayoutData( fData ); shell.open(); 

代码输出

您可以使用以下代码作为起点:

 public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout(1, false)); SashForm form = new SashForm(shell, SWT.HORIZONTAL); form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); Composite left = new Composite(form, SWT.BORDER); left.setLayout(new GridLayout(3, true)); left.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); for(int i = 0; i < 9; i++) { Button button = new Button(left, SWT.PUSH); button.setText("Button " + i); button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true)); } final Composite right = new Composite(form, SWT.BORDER); right.setLayout(new GridLayout(1, true)); right.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); Button fillButton = new Button(right, SWT.PUSH); fillButton.setText("Fill"); fillButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); /* Set the width to 80% and 20% */ form.setWeights(new int[] {4, 1}); shell.setSize(400, 400); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } 

它看起来像这样:

在此处输入图像描述


它基本上是一个包含两部分的SashForm 。 左边部分是一个GridLayout有三列,右边部分是一个GridLayout有一列。 无需混合Layout

使用form.setWeights(new int[] {4, 1});设置百分比form.setWeights(new int[] {4, 1});