添加背景图像按钮SWT [英] Adding background image to button in SWT

查看:237
本文介绍了添加背景图像按钮SWT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何添加背景图像按钮在SWT?我需要这样的东西在附件中。

Can anyone please tell me how to add background image to a button in SWT? I need something like in the attachment.

推荐答案

好吧,按钮不是,如果你基本上要的得出的最佳起点的一个完全不同类型的按钮。不过,您可以创建一个使用自己的小部件<一个href=\"http://www.eclipse.org/articles/Article-Writing%20Your%20Own%20Widget/Writing%20Your%20Own%20Widget.htm\"相对=nofollow>这个教程。我写到这里一个例子:

Alright, Button isn't the best starting point if you basically want to draw a completely different type of button. You can however, create your own widget using this tutorial. I wrote an example here:

public class ImageButton extends Composite
{
    private Color   textColor;
    private Image   image;
    private String  text;
    private int     width;
    private int     height;

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

        textColor = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);

        /* Add dispose listener for the image */
        addListener(SWT.Dispose, new Listener()
        {
            @Override
            public void handleEvent(Event arg0)
            {
                if (image != null)
                    image.dispose();
            }
        });

        /* Add custom paint listener that paints the stars */
        addListener(SWT.Paint, new Listener()
        {
            @Override
            public void handleEvent(Event e)
            {
                paintControl(e);
            }
        });

        /* Listen for click events */
        addListener(SWT.MouseDown, new Listener()
        {
            @Override
            public void handleEvent(Event arg0)
            {
                System.out.println("Click");
            }
        });
    }

    private void paintControl(Event event)
    {
        GC gc = event.gc;

        if (image != null)
        {
            gc.drawImage(image, 1, 1);
            Point textSize = gc.textExtent(text);
            gc.setForeground(textColor);
            gc.drawText(text, (width - textSize.x) / 2 + 1, (height - textSize.y) / 2 + 1, true);
        }
    }

    public void setImage(Image image)
    {
        this.image = new Image(Display.getDefault(), image, SWT.IMAGE_COPY);
        width = image.getBounds().width;
        height = image.getBounds().height;
        redraw();
    }

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

    @Override
    public Point computeSize(int wHint, int hHint, boolean changed)
    {
        int overallWidth = width;
        int overallHeight = height;

        /* Consider hints */
        if (wHint != SWT.DEFAULT && wHint < overallWidth)
            overallWidth = wHint;

        if (hHint != SWT.DEFAULT && hHint < overallHeight)
            overallHeight = hHint;

        /* Return computed dimensions plus border */
        return new Point(overallWidth + 2, overallHeight + 2);
    }

    public static void main(String[] args)
    {
        Display display = Display.getDefault();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, false));

        ImageButton button = new ImageButton(shell, SWT.NONE);
        button.setImage(new Image(display, "button.png"));
        button.setText("Button");

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

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

是这样的:

当然,你必须照顾不同状态的如 pressed 悬停等上。

Of course you'd have to take care of different states like pressed, hover and so on.

这篇关于添加背景图像按钮SWT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆