java - SWT : how to make rounded border label

标签 java swt

我试过了,它设置了一个新的简单边框。

我的简单代码是:

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class LabelBorder {

    public static void main (String [] args) {
        Display display = new Display ();
        Shell shell = new Shell(display);

        //here it makes simple border

        Label label = new Label(shell, SWT.BORDER);
        label.setSize(100,30);
        label.setLocation(50, 50);
        label.setText("I am a Label");


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

我想用标签和中心文本制作圆角边框和一些颜色

帮帮我。

最佳答案

这应该是一个很好的起点:

public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.setText("StackOverflow");

    final RoundedLabel label = new RoundedLabel(shell, SWT.NONE);
    label.setText("This is a label");

    shell.pack();
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }

    display.dispose();
}

private static class RoundedLabel extends Canvas
{
    private String           text   = "";
    private static final int MARGIN = 3;

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

        addPaintListener(new PaintListener()
        {
            @Override
            public void paintControl(PaintEvent e)
            {
                RoundedLabel.this.paintControl(e);
            }
        });
    }

    void paintControl(PaintEvent e)
    {
        Point rect = getSize();
        e.gc.fillRectangle(0, 0, rect.x, rect.x);
        e.gc.drawRoundRectangle(MARGIN, MARGIN, rect.x - 2 * MARGIN - 1, rect.y - 2 * MARGIN - 1, MARGIN, MARGIN);
        e.gc.drawText(text, 2 * MARGIN, 2 * MARGIN);
    }

    public String getText()
    {
        return text;
    }

    public void setText(String text)
    {
        this.text = text;
        redraw();
    }

    @Override
    public Point computeSize(int wHint, int hHint, boolean changed)
    {
        GC gc = new GC(this);

        Point pt = gc.stringExtent(text);

        gc.dispose();

        return new Point(pt.x + 4 * MARGIN, pt.y + 4 * MARGIN);
    }
}

看起来像这样:

enter image description here


随意调整边距

关于java - SWT : how to make rounded border label,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23402993/

相关文章:

java - SWT:如何进行高质量图像调整大小

java - 限制 ElementTreeSelectionDialog

java - 具有混合构造函数的 Spring Autowiring bean

java - SWT 进度条

java - 具有 Eclipse 外观的 Eclipse RCP 工具栏按钮

java - 在哪里可以找到 Java 的 com.drew jar?

java - swt DateTime setDate() 错误

java - 是否可以将字符串数组放入缓冲的阅读器中?

java - 在 Neo4J 中,如何在 Java 的密码查询中将标签设置为参数?

java - Java中使用线程进行套接字编程