gpt4 book ai didi

java - ScrolledComposite 垂直滚动条没有出现

转载 作者:行者123 更新时间:2023-11-29 07:32:25 27 4
gpt4 key购买 nike

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

编辑

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

在选择监听器中。

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

关于java - ScrolledComposite 垂直滚动条没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40182704/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com