swt标签顶部的额外空间(边框内)

时间:2014-08-06 20:24:24

标签: java user-interface swt

我正在创建一个标签,但当我在它周围画一个边框时,它表明边框内的数字正上方有一个小空格。我想制作标签,使边框接触所有边上的数字。 关于如何做的任何想法? 我这样做 -

    final Composite composite = new Composite(parent, SWT.NONE);
    final GridLayout layout = new GridLayout();
    layout.verticalSpacing = 0;
    layout.horizontalSpacing = 0;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    composite.setLayout(layout);
    composite.setLayoutData(new GridData(SWT.NONE, SWT.NONE, false, false));

    Label numberLabel = new Label(composite, SWT.BORDER);
    numberLabel.setLayoutData(new GridData(SWT.NONE, SWT.NONE, false, false));

我发现此网站上传了要共享的图片 - http://i59.tinypic.com/24mrhgp.png

1 个答案:

答案 0 :(得分:1)

您可以在画布上绘制标签以精确控制绘图。 Refer this article on canvas

调整方法e.gc.drawRectangle的参数以获得精细控制。

示例:

package testplugin;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class SWTHelloWorld {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);       
        shell.setLayout(new GridLayout());      
        final Composite composite = new Composite(shell, SWT.NONE);
        final GridLayout layout = new GridLayout();
        layout.verticalSpacing = 0;
        layout.horizontalSpacing = 0;
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        composite.setLayout(layout);
        composite.setLayoutData(new GridData(SWT.NONE, SWT.NONE, false, false));

        Label numberLabel = new Label(composite, SWT.BORDER);
        numberLabel.setLayoutData(new GridData(SWT.NONE, SWT.NONE, false, false));
        numberLabel.setText("84");

        Canvas canvas = new Canvas(shell, SWT.NO_REDRAW_RESIZE);            
        canvas.addPaintListener(new PaintListener() {

            @Override
            public void paintControl(PaintEvent e) {
                e.gc.drawString("84", 0, 0);
                Point pt = e.gc.stringExtent("84");             
                e.gc.drawRectangle(0, 2, pt.x-1, pt.y-4);
            }

        });

        shell.pack();
        shell.open();
        shell.setSize(200, 300);
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}