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 楼答案
最重要的是你错过了一个
在选择侦听器中
另外,您应该使用},使用
SWT.NONE
作为c1
{SWT.V_SCROLL
会产生一个不需要的额外滚动条因为您没有使用
setExpandVertical
,所以调用setMinSize
没有意义所以这是可行的: