有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ScrolledComposite垂直滚动条未出现

我试图创建一个容器,其中将包含一个按钮后,一定数量的按钮,我想得到一个滚动条,但目前滚动条没有出现任何建议

编辑

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

共 (1) 个答案

  1. # 1 楼答案

    最重要的是你错过了一个

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

    在选择侦听器中

    另外,您应该使用SWT.NONE作为c1{},使用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();
        }
    });