有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在不显示滚动条的情况下消除SWT ScrolledComposite中浪费的空间

我有一个水平的ScrolledComposite拿着两个按钮(实际上是一个复合按钮) 有两个按钮)。即使滚动条 酒吧是隐藏的。(当水平空间足够且 不需要滚动。)

enter image description here

我怎样才能摆脱这浪费的空间

同时,我配置了滚动组合,以始终显示 滚动条(setAlwaysShowScrollBars(true)),但我更喜欢 清除浪费的空间

enter image description here

谢谢你的考虑

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) 个答案

  1. # 1 楼答案

    存在“浪费空间”的原因是ScrolledComposite对象的首选大小包括滚动条的空间,无论滚动条是否可见。要禁用此默认功能,可以通过扩展ScrolledComposite来覆盖首选大小计算:

    class PackedScrolledComposite extends ScrolledComposite
    {
        Point scrollBarSize;  // Size of OS-specific scrollbar
    
        public PackedScrolledComposite(Composite parent, int style)
        {
            super(parent, style);
    
            Composite composite = new Composite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
            composite.setSize(1, 1);
            scrollBarSize = composite.computeSize(0, 0);
            composite.dispose();
        }
    
        public Point computeSize(int wHint, int hHint, boolean changed)
        {
            Point point = super.computeSize(wHint, hHint, changed);
            point.x += ((getStyle() & SWT.V_SCROLL) != 0) ? -scrollBarSize.x : 0;
            point.y += ((getStyle() & SWT.H_SCROLL) != 0) ? -scrollBarSize.y : 0;
    
            return point;
        }
    }