右边出现SWT Composite

SWT Composite appears on the right

我正在开发一个带有视图的 eclipse 插件,我需要使该视图可滚动。我找到了如何使其可滚动,但复合材料出现在视图的右侧。如何填充所有视图?

我的代码:

public class Comp2 extends Composite {

    public Comp2(Composite parent, int style) {
        super(parent, SWT.NONE);

        final ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
        sc.setLayout(new FillLayout());
        sc.setExpandHorizontal(true);
        sc.setExpandVertical(true);

        final Composite subContainer = new Composite(sc, SWT.NONE);

        Button btnNewButton = new Button(subContainer, SWT.NONE);
        btnNewButton.setBounds(75, 99, 90, 30);
        btnNewButton.setText("New Button");

        Button btnNewButton_1 = new Button(subContainer, SWT.NONE);
        btnNewButton_1.setBounds(357, 398, 90, 30);
        btnNewButton_1.setText("New Button");

        sc.setContent(subContainer);

        sc.addControlListener(new ControlAdapter() {

            @Override
            public void controlResized(ControlEvent e) {
                Rectangle r = sc.getClientArea();
                sc.setMinSize(subContainer
                        .computeSize(r.width, SWT.DEFAULT));
            }
        });
    }

    @Override
    protected void checkSubclass() {
        // Disable the check that prevents subclassing of SWT components
    }
}

由于您正在扩展 Composite 复合体正在将其正常主体添加到父级。然后,您将 ScrolledComposite 添加到 parent,因此它位于第一个组合旁边。

您可以通过更改其父级将 ScrollComposite 放入 Composite 中:

super(parent, SWT.NONE);

// This composite needs a layout
setLayout(new FillLayout());

// Parent of the ScrolledComposite is this Composite
ScrolledComposite sc = new ScrolledComposite(this, SWT.H_SCROLL | SWT.V_SCROLL);