java如何在不显示滚动条的情况下消除SWT ScrolledComposite中浪费的空间
我有一个水平的ScrolledComposite
拿着两个按钮(实际上是一个复合按钮)
有两个按钮)。即使滚动条
酒吧是隐藏的。(当水平空间足够且
不需要滚动。)
我怎样才能摆脱这浪费的空间
同时,我配置了滚动组合,以始终显示
滚动条(setAlwaysShowScrollBars(true)
),但我更喜欢
清除浪费的空间
谢谢你的考虑
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ScrollbarSpaceIssue extends Composite
{
public ScrollbarSpaceIssue(final Composite parent, final int style)
{
super(parent, style);
setLayout(new GridLayout(1, false));
final ScrolledComposite scrolledComposite = new ScrolledComposite(this, SWT.BORDER | SWT.H_SCROLL);
scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
//scrolledComposite.setAlwaysShowScrollBars(true);
{
final Composite composite = new Composite(scrolledComposite, SWT.NONE);
composite.setLayout(new GridLayout(2, false));
{
final Button btn1 = new Button(composite, SWT.NONE);
btn1.setText("Button 1");
final Button btn2 = new Button(composite, SWT.NONE);
btn2.setText("Button 2");
}
scrolledComposite.setContent(composite);
scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
pack();
}
public static void main(final String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Testcase");
shell.setLayout(new FillLayout());
new ScrollbarSpaceIssue(shell, SWT.NONE);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
# 1 楼答案
存在“浪费空间”的原因是
ScrolledComposite
对象的首选大小包括滚动条的空间,无论滚动条是否可见。要禁用此默认功能,可以通过扩展ScrolledComposite
来覆盖首选大小计算: