文字OVER图像的Java SWT按钮 [英] Java SWT Button with text OVER image

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

问题描述

SWT不支持在一个按钮在图像文本。当图像被设置,隐藏的文本。有什么能实现这一目标的最佳方法?

我能想到的最好的解决方法是合并与图像的文字...没有人知道一个良好的Java库做这个?

请注意:我发现,简单地绘制图像上的文字自定义实施方面: https://开头github.com/jrobichaux/SquareButton/wiki 的。这是为什么不SWT实现的? (反正我不能让这个类的工作......我真的,因为我想要本机按钮的外观不希望这种做法)


解决方案

  

这是为什么不SWT执行?


SWT的目的是找本地人。为了实现这一目标,SWT使用OS的资源的部件。因此,SWT 按钮的神色像OS按钮。文本上方或下方图片按钮并不是很常见。

我设法实现的东西,不知怎的作品,但它是相当哈克。也许你可以使用它作为一个起点:

 公共静态无效的主要(字串[] args){
    最终显示显示=新显示();
    最终壳牌=新的外壳(显示);
    shell.setLayout(新网格布局(1,FALSE));    最终图像的图像= display.getSystemImage(SWT.ICON_ERROR);
    最后字符串文本=按钮;    最后Button按钮=新按钮(外壳,SWT.PUSH);    数据的GridData =新的GridData();
    浮fontHeight = shell.getFont()getFontData()[0] .height。
    data.heightHint = image.getImageData()高度+(INT)fontHeight + 20。
    data.widthHint = 100;    button.setLayoutData(数据);    button.addListener(SWT.Paint,新的侦听器(){        @覆盖
        公共无效的handleEvent(事件的事件){
            GC GC = event.gc;            INT宽度=((的GridData)button.getLayoutData())widthHint。
            INT高度=((的GridData)button.getLayoutData())heightHint。
            点TEXTSIZE = gc.textExtent(文本);            gc.drawText(文字,宽度/ 2 - textSize.x / 2,image.getImageData()高度 - image.getImageData()高度/ 2 - textSize.y,真);
            gc.drawImage(图像,宽度/ 2 - image.getImageData()宽/ 2,高度/ 2 - image.getImageData()高度/ 2 + textSize.y / 2);
        }
    });    shell.pack();
    shell.open();
    而(!shell.isDisposed()){
        如果(!display.readAndDispatch()){
            display.sleep();
        }
    }
    image.dispose();
    display.dispose();
}

正如你所看到的,我用的GridData 来实现按钮的大小和监听器 SWT.Paint 与图像和文本,以填补该按钮。

结果是这样的:

SWT does not support "Text over Image" in a Button. When the Image is set, hides the text. What could be the best approach to achieve this?

The best workaround i could think of was to "merge" the text with the image... does anyone know a good Java library do to this?

NOTE: I've found this custom implementation that simply draws the Text over the Image: https://github.com/jrobichaux/SquareButton/wiki. Why is this not implemented in SWT?? (anyway, i could not make this class work... and i really don't want this approach because i want the NATIVE Button look)

解决方案

Why is this not implemented in SWT?

SWT aims at looking native. To achieve this, SWT uses OS resources for the widgets. Consequently, the SWT Buttons look like the OS buttons. Buttons with text above or below images are not quite common.

I managed to achieve something that somehow works, but it's quite hacky. Maybe you can use it as a starting point:

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

    final Image image = display.getSystemImage(SWT.ICON_ERROR);
    final String text = "Button";

    final Button button = new Button(shell, SWT.PUSH);

    GridData data = new GridData();
    float fontHeight = shell.getFont().getFontData()[0].height;
    data.heightHint = image.getImageData().height + (int)fontHeight + 20;
    data.widthHint = 100;

    button.setLayoutData(data);

    button.addListener(SWT.Paint, new Listener() {

        @Override
        public void handleEvent(Event event) {
            GC gc = event.gc;

            int width = ((GridData)button.getLayoutData()).widthHint;
            int height = ((GridData)button.getLayoutData()).heightHint;
            Point textSize = gc.textExtent(text);

            gc.drawText(text, width / 2 - textSize.x / 2, image.getImageData().height - image.getImageData().height / 2 - textSize.y, true);
            gc.drawImage(image, width / 2 - image.getImageData().width / 2, height / 2 - image.getImageData().height / 2 + textSize.y / 2);
        }
    });

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

As you can see, I used GridData to achieve the sizing of the button and a Listener for SWT.Paint to fill the button with the image and the text.

The result looks like this:

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

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