【问题标题】:Adding background image to button in SWT将背景图像添加到 SWT 中的按钮
【发布时间】:2014-07-06 22:55:11
【问题描述】:

谁能告诉我如何在 SWT 中为按钮添加背景图片?我需要类似附件中的东西。

【问题讨论】:

    标签: java button swt background-image


    【解决方案1】:

    好吧,Button 不是您想要绘制完全不同类型的按钮的最佳起点。但是,您可以使用 this 教程创建自己的小部件。我在这里写了一个例子:

    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();
        }
    }
    

    看起来像这样:


    当然,您必须处理不同的状态,例如 pressedhover 等等。

    【讨论】:

    • 非常感谢巴兹。这正是我一直在寻找的。但是此图像按钮与单击时的按下行为不同。你能告诉我这是如何实现的吗?我应该覆盖哪个事件?
    • @JayakrishnanSalim 好吧,您必须监听 SWT.MouseDownSWT.MouseUp 并使用不同的 Image 触发重绘。一定要dispose() all Images 你创建的。
    • 好的。非常感谢您的支持。 :)
    • @JayakrishnanSalim 那到底什么会起作用?究竟是什么不起作用?与其在评论部分讨论这个问题,不如提出一个新问题。
    • 我有一个小的上传图片,需要在列表的每一行中显示。我使用了您共享的相同代码 sn-p 并且一切正常。但是当行数增加到 1000 时,我得到了 'org.eclipse.swt.SWTError: No more handles' 异常。我用了Sleak.java,发现图像对象在1400左右。
    猜你喜欢
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 2020-12-09
    相关资源
    最近更新 更多