我想要一个ScrolledComposite,其父网格有GridLayout,但滚动条没有显示,除非我使用FillLayout.我对FillLayout的问题是它的子节点占用了可用空间的相等部分.
在我的例子中有两个小部件,顶部的小部件不应超过窗口的1/4,ScrolledComposite应占用剩余空间.但是,它们都占了一半.
有没有办法将GridLayout与ScrolledComposite一起使用,还是可以修改FillLayout的行为?
这是我的代码:
private void initContent() { //GridLayout shellLayout = new GridLayout(); //shellLayout.numColumns = 1; //shellLayout.verticalSpacing = 10; //shell.setLayout(shellLayout); shell.setLayout(new FillLayout(SWT.VERTICAL)); searchComposite = new SearchComposite(shell,SWT.NONE); searchComposite.getSearchButton().addListener(SWT.Selection,this); ScrolledComposite scroll = new ScrolledComposite(shell,SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER); scroll.setLayout(new GridLayout(1,true)); Composite scrollContent = new Composite(scroll,SWT.NONE); scrollContent.setLayout(new GridLayout(1,true)); for (ChangeDescription description : getChanges(false)) { ChangesComposite cc = new ChangesComposite(scrollContent,description); } scroll.setMinSize(scrollContent.computeSize(SWT.DEFAULT,SWT.DEFAULT)); scroll.setContent(scrollContent); scroll.setExpandVertical(true); scroll.setExpandHorizontal(true); scroll.setAlwaysShowScrollBars(true); }
解决方法
除了setLayout()之外,还需要调用setLayoutData().在下面的代码示例中,看看如何构造GridData对象并将其传递给两个setLayoutData()调用中的每一个.
private void initContent(Shell shell) { // Configure shell shell.setLayout(new GridLayout()); // Configure standard composite Composite standardComposite = new Composite(shell,SWT.NONE); standardComposite.setLayoutData(new GridData(SWT.FILL,SWT.TOP,true,false)); // Configure scrolled composite ScrolledComposite scrolledComposite = new ScrolledComposite(shell,SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER); scrolledComposite.setLayout(new GridLayout()); scrolledComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true)); scrolledComposite.setExpandVertical(true); scrolledComposite.setExpandHorizontal(true); scrolledComposite.setAlwaysShowScrollBars(true); // Add content to scrolled composite Composite scrolledContent = new Composite(scrolledComposite,SWT.NONE); scrolledContent.setLayout(new GridLayout()); scrolledComposite.setContent(scrolledContent); }