0

我已经按照 gwt 中的这篇 ImageButton 帖子的说明创建了我自己的圆形按钮,它在视觉上看起来还不错。我现在要做的是为这个新类添加点击处理程序。我该怎么办?我是否需要两个图像和标签的点击处理程序?然后我必须怎么做才能使用 CSS 配置这个组件?

到目前为止,这是我的代码。

    public class RoundButton extends Composite
    {
        protected final Label textHolder = new Label("helasdfasdf");
        protected final Image leftSide = new Image( "/icons/left.ico" );
        protected final Image rightSide = new Image( "/icons/right.ico" );
        protected final HorizontalPanel contentTable;

        public RoundButton()
        {
            contentTable = new HorizontalPanel();
            initWidget( contentTable );
            contentTable.add(leftSide);
            contentTable.add(textHolder);
            contentTable.add(rightSide);
        }

        public void setText( String text )
        {
            textHolder.setText(text);
        }   
    }
4

2 回答 2

1

为了使您的 RoundButton 更加标准化,我将实现com.google.gwt.event.dom.client.HasClickHandlers界面并将单击事件添加到整个组件(即包装器小部件):

public class RoundButton extends Composite implemens HasClickHandlers
{
    protected final Label textHolder = new Label("helasdfasdf");
    protected final Image leftSide = new Image( "/icons/left.ico" );
    protected final Image rightSide = new Image( "/icons/right.ico" );
    protected final HorizontalPanel contentTable;

    public RoundButton()
    {
        contentTable = new HorizontalPanel();
        initWidget( contentTable );
        contentTable.add(leftSide);
        contentTable.add(textHolder);
        contentTable.add(rightSide);
    }

    public void setText( String text )
    {
        textHolder.setText(text);
    }   


    public HandlerRegistration addClickHandler(ClickHandler handler)
    {
        return addDomHandler(handler, ClickEvent.getType());         
    }  

}
于 2013-03-12T11:39:39.727 回答
0

您可以像这样捕捉复合材料上的点击:

public RoundButton()
{
    ...
    sinkEvents(Event.ONCLICK);
}

@Override
public void onBrowserEvent(Event event)
{
    super.onBrowserEvent(event);
    if (DOM.eventGetType(event) == Event.ONCLICK)
    {
        ...
    }
}
于 2013-03-12T11:39:30.307 回答