ScrolledComposite 的 setMinSize一般设为子控件的大小,这样可以保证滚动条的范围就是子控件的范围
代码如下,
public class Scrolled { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout(2,false)); //shell.setLayout(new FillLayout(SWT.HORIZONTAL)); Composite content = new Composite(shell, SWT.BORDER); // content.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false)); content.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true)); Composite sidebar = new Composite(shell, SWT.BORDER); sidebar.setLayout(new FillLayout(SWT.VERTICAL)); // sidebar.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false)); sidebar.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,true)); Composite cc = new Composite(sidebar, SWT.BORDER); ScrolledComposite sc = new ScrolledComposite(sidebar, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); sc.setLayout(new GridLayout(1,true)); Composite c = new Composite(sc, SWT.NONE); // c.setSize(400, 400); c.setLayout(new GridLayout(1, true)); for(int i = 0; i < 1000; i++){ new Button(c, SWT.PUSH).setText("Text"); } sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT)); sc.setContent(c); sc.setExpandHorizontal(true); sc.setExpandVertical(true); sc.setAlwaysShowScrollBars(true); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }