防止SWT滚动复合材料吃它的部分孩子

时间:2021-12-29 10:59:14

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: 防止SWT滚动复合材料吃它的部分孩子

…但它夹住了最后一个按钮:

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.

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

4 个解决方案

#1


12  

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 minimum height of the content, as long as the width is the same.

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

#2


2  

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

#3


0  

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

您不需要在布局之后重新计算滚动框的大小吗?

#4


0  

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,并将其传递给主组合的大小。

#1


12  

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 minimum height of the content, as long as the width is the same.

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

#2


2  

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

#3


0  

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

您不需要在布局之后重新计算滚动框的大小吗?

#4


0  

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,并将其传递给主组合的大小。