简体   繁体   English

在gwt中创建自定义按钮

[英]creating custom button in 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. 我正在尝试与GWT做一些非常常见的事情 - 通过将文本放在图像顶部来创建带有图像和文本的按钮行为。

I have used the HTML widget but how can I make the text not selectable? 我已经使用了HTML小部件,但是如何才能使文本无法选择?

I recently had the same need for a GWT button which allows to add image AND text. 我最近对GWT按钮的需求相同,它允许添加图像和文本。 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;
    }
}

Usage with UiBinder XML definition 用于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: 这样一个按钮的截图:
替代文字

Do you mean to get rid of the text select cursor, or make the text completely unselectable? 你的意思是摆脱文本选择光标,或使文本完全不可选?

To make it look like something clickable, you can use the cursor CSS rule. 要使其看起来像可点击的东西,您可以使用光标CSS规则。

.widget_style {cursor: pointer;}

Actually making it unselectable is not well supported from what I understand. 实际上让它无法选择并不能得到我理解的支持。 It is in the CSS3 specs with user-select. 它是CSS3规范中的用户选择。

.widget_style {user-select:none;}

A class that allows the addition of arbitrary widgets to a Button: 允许向Button添加任意小部件的类:

import java.util.ArrayList;
import java.util.Iterator;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class ButtonWithWidgets extends Button implements HasWidgets
{
    private ArrayList<Widget> widgets = new ArrayList<Widget>();

    @Override
    public void add(Widget w)
    {
        DOM.insertChild(getElement(), w.getElement(), widgets.size());
    }

    @Override
    public void clear()
    {
        for (Widget widget : widgets)
        {
            DOM.removeChild(getElement(), widget.getElement());
        }
        widgets.clear();
    }

    @Override
    public Iterator<Widget> iterator()
    {
        return widgets.iterator();
    }

    @Override
    public boolean remove(Widget w)
    {
        if (widgets.indexOf(w) != -1)
        {
            widgets.remove(w);
            DOM.removeChild(getElement(), w.getElement());
            return true;
        }
        else
            return false;
    }

}

with UiBinder: 与UiBinder:

... ...

<customwidgets:ButtonWithWidgets>
    <g:Label>Whatever</g:Label>
    <g:Image url="etc.png"/>
</customwidgets:ButtonWithWidgets>

I would use the Button-Widget and call the setHTML() function. 我会使用Button-Widget并调用setHTML()函数。 You could use this code: 你可以使用这段代码:

public class Custombutton extends Button {
    public CustomButton(String text, String img) {
        this.setHTML(text + "<br><img src=\"" + img + "\">");
    }     
}

You just have to provide the text and the img url when you create the button. 您只需在创建按钮时提供文本和img URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM