到目前为止我尝试了什么:
在 createPartControl 中:
ScrolledComposite sc = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);
sc.setLayoutData(new GridData(GridData.FILL_BOTH));
sc.setExpandVertical(true);
sc.setExpandHorizontal(true);
sc.setSize(ApplicationWorkbenchWindowAdvisor.WIDTH, ApplicationWorkbenchWindowAdvisor.HEIGHT);
final TabFolder tabFolder = new TabFolder(sc, SWT.TOP);
但这行不通。我的问题是,如果我调整程序窗口的大小,滚动条不会出现在我的 View 中。有什么想法吗?
最佳答案
通常在 Eclipse View 中,我希望我的控件占用所有可用空间,并且只显示滚动条,否则控件将缩小到可用大小以下。
其他答案完全有效,但我想添加一个完整的 createPartControl
方法示例 (Eclipse e4)。
@PostConstruct
public void createPartControl(Composite parent) {
ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
Composite composite = new Composite(sc, SWT.NONE);
sc.setContent(composite);
composite.setLayout(new GridLayout(2, false));
Label label = new Label(composite, SWT.NONE);
label.setText("Foo");
Text text = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
GridDataFactory.fillDefaults().grab(true, true).hint(400, 400).applyTo(text);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
请注意,.fillDefaults()
意味着 .align(SWT.FILL, SWT.FILL)
。
我经常使用这种模式,所以我创建了以下小辅助方法:
public static ScrolledComposite createScrollable(Composite parent, Consumer<Composite> scrollableContentCreator) {
ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
Composite composite = new Composite(sc, SWT.NONE);
sc.setContent(composite);
scrollableContentCreator.accept(composite);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
return sc;
}
感谢 Java 8 lambda,您现在可以以非常紧凑的方式实现新的可滚动组合:
createScrollable(container, composite -> {
composite.setLayout(new FillLayout());
// fill composite with controls
});
关于java - 如何在 Eclipse RCP 应用程序中使 View 可滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7791696/