防止SWT ScrolledComposite吃掉它的一部分孩子

我做错了什么?

以下是我的代码的摘录:

public void createPartControl(Composite parent) { parent.setLayout(new FillLayout()); ScrolledComposite scrollBox = new ScrolledComposite(parent, SWT.V_SCROLL); scrollBox.setExpandHorizontal(true); mParent = new Composite(scrollBox, SWT.NONE); scrollBox.setContent(mParent); FormLayout layout = new FormLayout(); mParent.setLayout(layout); // Adds a bunch of controls here mParent.layout(); mParent.setSize(mParent.computeSize(SWT.DEFAULT, SWT.DEFAULT, true)); } 

…但它会剪辑最后一个按钮: alt text

bigbrother82:那没用。

SCdF:我尝试了你的建议,现在滚动条消失了。 我还需要做更多的工作。

这是使用ScrolledComposite时的常见障碍。 当它变得如此之小以至于必须显示滚动条时,客户端控件必须水平缩小以为滚动条腾出空间。 这具有使一些标签包裹线的副作用,其将下面的控件向下移动,这增加了内容合成所需的最小高度。

您需要侦听内容合成( mParent )的宽度更改,在给定新内容宽度的情况下再次计算最小高度,并在具有新高度的滚动复合上调用setMinHeight()

 public void createPartControl(Composite parent) { parent.setLayout(new FillLayout()); ScrolledComposite scrollBox = new ScrolledComposite(parent, SWT.V_SCROLL); scrollBox.setExpandHorizontal(true); scrollBox.setExpandVertical(true); // Using 0 here ensures the horizontal scroll bar will never appear. If // you want the horizontal bar to appear at some threshold (say 100 // pixels) then send that value instead. scrollBox.setMinWidth(0); mParent = new Composite(scrollBox, SWT.NONE); FormLayout layout = new FormLayout(); mParent.setLayout(layout); // Adds a bunch of controls here mParent.addListener(SWT.Resize, new Listener() { int width = -1; public void handleEvent(Event e) { int newWidth = mParent.getSize().x; if (newWidth != width) { scrollBox.setMinHeight(mParent.computeSize(newWidth, SWT.DEFAULT).y); width = newWidth; } } } // Wait until here to set content pane. This way the resize listener will // fire when the scrolled composite first resizes mParent, which in turn // computes the minimum height and calls setMinHeight() scrollBox.setContent(mParent); } 

在侦听大小更改时,请注意我们忽略宽度保持不变的任何resize事件。 这是因为只要宽度相同,内容高度的变化不会影响内容的最小高度。

如果我没有弄错你需要换掉

 mParent.layout(); 

 mParent.setSize(mParent.computeSize(SWT.DEFAULT, SWT.DEFAULT, true)); 

这样你就拥有:

 public void createPartControl(Composite parent) { parent.setLayout(new FillLayout()); ScrolledComposite scrollBox = new ScrolledComposite(parent, SWT.V_SCROLL); scrollBox.setExpandHorizontal(true); mParent = new Composite(scrollBox, SWT.NONE); scrollBox.setContent(mParent); FormLayout layout = new FormLayout(); mParent.setLayout(layout); // Adds a bunch of controls here mParent.setSize(mParent.computeSize(SWT.DEFAULT, SWT.DEFAULT, true)); mParent.layout(); } 

布局后你不需要重新计算scrollBox的大小吗?

完成布局后,尝试在ScrolledComposite上设置.setMinWidth和.setMinHeight,并将其传递给主要合成的大小。