如何在gridlayout中使用滚动合成启用滚动?

时间:2015-01-13 14:39:04

标签: java layout swt

如果我使用shell,下面的代码工作正常。但是我使用的是复合材料无法正常工作。

如果我们使用shell,

代码工作正常:

   Display display = new Display ();
    Shell shell = new Shell (display);
    shell.setLayout(new GridLayout());  
    final ScrolledComposite sc = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    Composite c = new Composite(sc, SWT.NONE);
    c.setLayout(new GridLayout(10, true));
    for (int i = 0 ; i < 300; i++) {
        Button b = new Button(c, SWT.PUSH);
        b.setText("Button "+i);
    }
    sc.setContent(c);
    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    sc.setShowFocusedControl(true);
    shell.setSize(300, 500);
    shell.open ();
    while (!shell.isDisposed ()) {
        if (!display.readAndDispatch ()) display.sleep ();
    }
    display.dispose ();
如果我们在gridlayout中使用复合,

相同的代码不起作用:

   Display display = new Display ();
    Shell shell = new Shell (display);
    shell.setLayout(new GridLayout());
    Composite c1 = new Composite(shell, SWT.NONE);
    c1.setLayout(new GridLayout());
    final ScrolledComposite sc = new ScrolledComposite(c1, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);       
    sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    Composite c = new Composite(sc, SWT.NONE);
    c.setLayout(new GridLayout(10, true));
    for (int i = 0 ; i < 300; i++) {
        Button b = new Button(c, SWT.PUSH);
        b.setText("Button "+i);
    }
    sc.setContent(c);
    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    sc.setShowFocusedControl(true);
    shell.setSize(300, 500);
    shell.open ();
    while (!shell.isDisposed ()) {
        if (!display.readAndDispatch ()) display.sleep ();
    }
    display.dispose ();

1 个答案:

答案 0 :(得分:1)

您没有告诉您的c1综合报告它与其父级相关的行为(即设置布局数据)。

您有两种选择:

c1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

shell.setLayout(new FillLayout());