Java swt标签顶部额外空间(边框内)
我正在创建一个标签,但当我在其周围绘制边框时,它显示在边框内的数字正上方有一个小空间。我想做标签,这样的边界接触的数字在所有方面。 有什么办法吗? 我正在这样做-Java swt标签顶部额外空间(边框内),java,user-interface,swt,Java,User Interface,Swt,我正在创建一个标签,但当我在其周围绘制边框时,它显示在边框内的数字正上方有一个小空间。我想做标签,这样的边界接触的数字在所有方面。 有什么办法吗? 我正在这样做- final Composite composite = new Composite(parent, SWT.NONE); final GridLayout layout = new GridLayout(); layout.verticalSpacing = 0; layout.horizontalSp
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));
我发现这个网站上传了一张要共享的图片-你可以在画布上画标签,以更好地控制绘图 调整方法
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();
}
}
谢谢你的链接和建议。我将不能使用这个矩形的大小将在不同的分辨率和机器不同。我想有一个更简单的方法,因为有一个图像标签,如果我在它周围画边框,它表明它能很好地接触到所有的边。