簡體   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