繁体   English   中英

ScrolledComposite垂直滚动条未出现

[英]ScrolledComposite vertical scrollbar not appearing

我正在尝试创建一个容器,在要获得滚动条的一定数量的按钮之后,该容器中将包含一个按钮,但是现在滚动条没有出现任何建议?

编辑

MoBuConLTLUI类

public class MoBuConLTLUI extends Composite {

      public MoBuConLTLUI(Composite parent) {
          super(parent, SWT.NONE);
          this.setLayout(new GridLayout());
          createScrollableComposite(parent);
      }

      public void createScrollableComposite (Composite parent) {
          // set the size of the scrolled content - method 1
          final ScrolledComposite sc1 = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
          final Composite c1 = new Composite(sc1, SWT.V_SCROLL);
          sc1.setContent(c1);
          sc1.setMinSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
          GridLayout layout = new GridLayout();
          layout.numColumns = 1;
          c1.setLayout(layout);
          Button b1 = new Button (c1, SWT.PUSH);
          b1.setText("first button");
          c1.setSize(200,200);


          Button add = new Button (parent, SWT.PUSH);
          add.setText("add children");
          final int[] index = new int[]{0};
          add.addListener(SWT.Selection, new Listener() {
              public void handleEvent(Event e) {
                  index[0]++;
                  Button button = new Button(c1, SWT.PUSH);
                  button.setText("button "+index[0]);
                  // reset size of content so children can be seen - method 1
                  c1.layout();
              }
          });
     }
}

编辑父级初始化如下

public class TestBundleUI extends AbstractEntryPoint{
   private static final long serialVersionUID = -7954149221017272321L;
   private Composite testUiParentComposite;
   @Override
   public void createContents(Composite parent) {
      this.testUiParentComposite = parent;
      testUiParentComposite.setLayout(new GridLayout());
      new MoBuConLTLUI(parent);
   }
 }

最主要的是您缺少一个

c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));

在选择监听器中。

您还应该使用SWT.NONEc1 Composite ,使用SWT.V_SCROLL给出了一个不必要的额外的滚动条。

由于您没有使用setExpandVertical因此在setMinSize没有意义。

所以这工作:

final ScrolledComposite sc1 = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
final Composite c1 = new Composite(sc1, SWT.NONE);
sc1.setContent(c1);
final GridLayout layout = new GridLayout();
layout.numColumns = 1;
c1.setLayout(layout);
final Button b1 = new Button (c1, SWT.PUSH);
b1.setText("first button");
c1.setSize(200,200);

final Button add = new Button(parent, SWT.PUSH);
add.setText("add children");
final int[] index = new int[]{0};
add.addListener(SWT.Selection, new Listener() {
    public void handleEvent(final Event e) {
        index[0]++;
        final Button button = new Button(c1, SWT.PUSH);
        button.setText("button "+index[0]);
        // reset size of content so children can be seen - method 1
        c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        c1.layout();
    }
});

您的代码中缺少三件事:

  1. 您的Composite不应使用SWT.V_SCROLL作为样式。 只需使用SWT.NONE

  2. 您应该在ScrolledComposite上调用以下命令:

     sc1.setExpandHorizontal(true); sc1.setExpandVertical(true); 
  3. 更新侦听器中的最小大小:

     c1.layout(); sc1.setMinSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 

尝试更改侦听器的实现。

scrolledComposite.addControlListener(new ControlAdapter() {
            @Override
            public void controlResized(ControlEvent e) {
                scrolledComposite.setMinSize(cs1.computeSize(scrolledComposite.getClientArea().width, SWT.DEFAULT));
            }
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM