2013-05-29 52 views
0

我所需要的只是一個gwt PushButton,除了文本以外我需要它具有圖像。GWT自定義PushButton不能與ImageResources一起工作

我使用GWT,GWTP和UiBinder的

在我ui.xml文件,下面的代碼工作,但如果我將鼠標懸停我不能指定圖像,或點擊按鈕

<g:SimplePanel height="100%" styleName="{style.button}" 
    width="100%"> 
    <g:HTMLPanel width="100%" height="100%"> 
     <table align="center"> 
      <tr> 
       <td> 
        <g:Image styleName="{style.pane}" resource='{res.Edit}' /> 
       </td> 
       <td> 
        <g:Label ui:field="label2" text="Edit Project Edit Project" /> 
       </td> 
      </tr> 
     </table> 
    </g:HTMLPanel> 
</g:SimplePanel> 

尋找解決的辦法後,我遇到了這個代碼here

package com.test.gwtptestproject.client; 

import com.google.gwt.dom.client.Style; 
import com.google.gwt.dom.client.Style.Float; 
import com.google.gwt.dom.client.Style.Unit; 
import com.google.gwt.resources.client.ImageResource; 
import com.google.gwt.safehtml.shared.SafeHtml; 
import com.google.gwt.user.client.DOM; 
import com.google.gwt.user.client.Element; 
import com.google.gwt.user.client.ui.Image; 

public class MyButton implements SafeHtml { 

    private static final long serialVersionUID = 1L; 
    Image img; 
    private String text; 
    private String innerHtml = ""; 

    public void setImage(ImageResource imgr) { 
     this.img = new Image(imgr); 
     update(); 
    } 

    public void setText(String text) { 
     this.text = text; 
     update(); 
    } 

    private void update() { 
     Element d = DOM.createDiv(); 
     if (img != null) { 
      Element di = DOM.createDiv(); 
      Style s = di.getStyle(); 
      s.setPaddingLeft(5, Unit.PX); 
      s.setPaddingRight(5, Unit.PX); 
      s.setFloat(Float.LEFT); 
      di.appendChild(img.getElement()); 
      d.appendChild(di); 
     } 
     if (text != null) { 
      Element t = DOM.createDiv(); 
      t.setInnerText(text); 
      d.appendChild(t); 
     } 
     innerHtml = d.getInnerHTML(); 

    } 

    public MyButton() { 
    } 

    @Override 
    public String asString() { 
     return innerHtml; 
    } 
} 

這是我後來在我的ui.xml像這樣使用

... 
<ui:with field='res' type='com.test.gwtptestproject.resources.ImageResources' /> 
... 
<g:PushButton> 
    <g:upFace> 
    <MyButton image="{res.Edit}" text="Edit"></MyButton> 
    </g:upFace> 
</g:PushButton> 

我ImageResources包包含以下界面,這個包被添加到來源gwt.xml文件

public interface ImageResources extends ClientBundle { 
    public ImageResources INSTANCE = GWT.create(ImageResources.class); 

    @Source("Edit.png") 
    ImageResource Edit(); 

    @Source("Edit_Hover.png") 
    ImageResource Edit_Hover(); 

    @Source("Edit_Click.png") 
    ImageResource Edit_Click(); 
} 

當我嘗試打開我一直有以下錯誤的設計師,我不知道如何解決這個問題(我得到同樣的錯誤,如果我嘗試從瀏覽器訪問該頁面)

[ERROR] java.lang.String required, but {res.Edit} returns com.google.gwt.resources.client.ImageResource: <MyButton image='{res.Edit}' text='Edit'> (:110) 
[ERROR] Deferred binding failed for 'com.test.gwtptestproject.client.SecondView.Binder'; expect subsequent failures 

據我瞭解,當我寫image="{res.Edit}"應用程序應該期待一個ImageResource而不是字符串ISN」它呢?

*注:我是很新,web開發(學校項目),所以不要假定我可能已經知道一些事情的時候,你會回答,除非明顯

回答

0

不要忘了包括MyButton的包作爲xmnls(XML命名空間)屬性開口ui:Binder標籤:

<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder' 
      xmlns:g='urn:import:com.google.gwt.user.client.ui' 
      xmlns:mybtn='urn:import:com.test.gwtptestproject.client'> 

和前綴您<MyButton>標籤與xmnls「別名(在這裏,mybtn):

<g:PushButton> 
    <g:upFace> 
    <mybtn:MyButton image="{res.Edit}" text="Edit"></mybtn:MyButton> 
    </g:upFace> 
</g:PushButton> 

這樣,GWT就會知道在哪裏看(package com.test.gwtptestproject.client)以查找MyButton類。

+0

這給了我同樣的錯誤 – Sim

相關問題