在 gwt 中创建自定义按钮 [英] creating custom button in gwt

查看:25
本文介绍了在 gwt 中创建自定义按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 GWT 做一些非常常见的事情 - 通过将文本定位在图像顶部来创建带有图像和文本的按钮行为.

I am trying to do something pretty common with GWT - creating a button behavior with an image and a text by positioning the text on top of the image.

我使用了 HTML 小部件,但如何使文本无法选择?

I have used the HTML widget but how can I make the text not selectable?

推荐答案

我最近同样需要一个 GWT 按钮,它允许添加图像和文本.所以我自己编码了一个,因为已经可用的实现不起作用.我在我的博客上写了一篇文章但我也在这里复制代码:

I recently had the same need for a GWT button which allows to add image AND text. So I coded one myself since the already available implementations didn't work. I wrote a post on my blog but I also copy the code here:

这是我的自定义按钮的代码

Here's the code for my custom button

import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Image;

public class CustomButton extends Button {
    private String text;

    public CustomButton(){
        super();
    }

    public void setResource(ImageResource imageResource){
        Image img = new Image(imageResource);
        String definedStyles = img.getElement().getAttribute("style");
        img.getElement().setAttribute("style", definedStyles + "; vertical-align:middle;");
        DOM.insertBefore(getElement(), img.getElement(), DOM.getFirstChild(getElement()));
    }

    @Override
    public void setText(String text) {
        this.text = text;
        Element span = DOM.createElement("span");
        span.setInnerText(text);
        span.setAttribute("style", "padding-left:3px; vertical-align:middle;");

        DOM.insertChild(getElement(), span, 0);
    }

    @Override
    public String getText() {
        return this.text;
    }
}

与 UiBinder XML 定义一起使用

...
<!-- ImageBundle definition -->
<ui:with field="res" type="com.sample.client.IDevbookImageBundle" />
...
<d:CustomButton ui:field="buttonSave" text="Save" resource="{res.save}"></d:CustomButton>

这样一个按钮的截图:

The screenshot of such a button:

这篇关于在 gwt 中创建自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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