这是使用 时常见的障碍。当它变得太小以至于必须显示滚动条时,客户端控件必须水平收缩才能为滚动条腾出空间。这会产生使某些标签换行的副作用,这会将以下控件向下移动得更远,从而增加了内容复合所需的最小高度。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);
}
在侦听大小更改时,请注意,我们忽略宽度保持不变的任何调整大小事件。这是因为只要宽度相同,内容高度的变化不会影响内容的最小高度。