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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:08:24  来源:igfitidea点击:

Prevent SWT ScrolledComposite from eating part of it's children

javaeclipseswtrcp

提问by Jeremy

What did I do wrong?

我做错了什么?

Here is an excerpt from my code:

这是我的代码的摘录:

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));
}

...but it clips the last button: alt text

...但它剪辑了最后一个按钮: 替代文字

bigbrother82: That didn't work.

bigbrother82:那没用。

SCdF: I tried your suggestion, and now the scrollbars are gone. I need to work some more on that.

SCdF:我尝试了你的建议,现在滚动条不见了。我需要在这方面做更多的工作。

采纳答案by qualidafial

This is a common hurdle when using ScrolledComposite. When it gets so small that the scroll bar must be shown, the client control has to shrink horizontally to make room for the scroll bar. This has the side effect of making some labels wrap lines, which moved the following controls farther down, which increased the minimum height needed by the content composite.

这是使用ScrolledComposite. 当它变得太小以致于必须显示滚动条时,客户端控件必须水平收缩以为滚动条腾出空间。这会产生一些副作用,使一些标签换行,从而将以下控件向下移动,从而增加了内容组合所需的最小高度。

You need to listen for width changes on the content composite (mParent), compute the minimum height again given the new content width, and call setMinHeight()on the scrolled composite with new height.

您需要监听内容组合 ( 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);
}

In listening for size changes, note that we ignore any resize events where the width stays the same. This is because changes in the height of the content do not affect the minimumheight of the content, as long as the width is the same.

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

回答by Jacob Schoen

If I am not mistaken you need to swap the

如果我没记错的话,你需要交换

mParent.layout();

and

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

so that you have:

所以你有:

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();
}

回答by alexmcchessers

Try setting .setMinWidth and .setMinHeight on the ScrolledComposite once the layout has been done, passing it the size of the main composite.

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

回答by SCdF

Don't you need to recompute the size of the scrollBox after the layout?

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