我有一个基于 Composite 的类,它嵌入了一个SWT List 实例。使用默认设置,我的WinXP系统上的列表高五行。如果不依赖于硬编码像素值或DPI设置等,如何将列表(以及周围复合材料)的高度设置为固定数量的行(例如3),而不添加任何内边距?

public FileSetBox(Composite parent, int style)
{
    super(parent, style);

    setLayout(new FillLayout());

    this.list = new List(this, SWT.V_SCROLL);

    ...
}

<强>更新

以下工作,但它没有考虑边框添加的高度,这导致最后一行的部分被覆盖。任何想法如何计算呢?

public FileSetBox(Composite parent, int style)
{
    ...
    GC gc = new GC(this);
    gc.setFont(this.list.getFont());
    this.preferredHeight = gc.getFontMetrics().getHeight() * 3;
    gc.dispose();
    ...
}

@Override
public Point computeSize(int arg0, int arg1)
{
    Point size = super.computeSize(arg0, arg1);
    return new Point(size.x, this.preferredHeight);
}
有帮助吗?

解决方案

你不能使用list.getBorderWidth()和list.getItemHeight()来获取高度吗?

其他提示

public FileSetBox(Composite parent, int style)
{
    super(parent, style);

    setLayout(new GridLayout(1, false));

    this.list = new List(this, SWT.V_SCROLL);

    GridData data = new GridData(GridData.FILL_BOTH);
    data.heightHint = 10 * ((List)control).getItemHeight(); // height for 10 rows
    data.widthHint = getStringWidth(25, list); // width enough to display 25 chars
    list.setLayoutData(data);

    ...
}

    public static int getStringWidth(int nChars, Control control){
        GC gc = new GC(control);
        gc.setFont(control.getFont());
        FontMetrics fontMetrics = gc.getFontMetrics();
        gc.dispose();
        return nChars * fontMetrics.getAverageCharWidth();
    }

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top