我正在尝试创建一个容器,在我想要获得滚动条的一定数量的按钮之后会包含一个按钮但是此刻滚动条没有出现任何建议?
编辑
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);
}
}
答案 0 :(得分:3)
最重要的是你错过了
c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
选择监听器中的。
此外,您应该使用SWT.NONE
c1
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 :(得分:2)
您的代码中缺少三件事:
您的Composite
不应使用SWT.V_SCROLL
作为样式。只需使用SWT.NONE
。
您应该在ScrolledComposite
上调用以下内容:
sc1.setExpandHorizontal(true);
sc1.setExpandVertical(true);
更新侦听器中的最小尺寸:
c1.layout();
sc1.setMinSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
答案 2 :(得分:0)
尝试更改侦听器实现。
scrolledComposite.addControlListener(new ControlAdapter() {
@Override
public void controlResized(ControlEvent e) {
scrolledComposite.setMinSize(cs1.computeSize(scrolledComposite.getClientArea().width, SWT.DEFAULT));
}
});