java - GWT UIBinder 方法返回类型 void?

标签 java gwt uibinder

下面是来自 Google's UIBinder tutorial 的一些 Java 代码。此代码与单独的 HTML 页面一起显示文本“Hello, World”。

public class HelloWorld {
    interface MyUiBinder extends UiBinder<DivElement, HelloWorld> {}
    private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

    @UiField SpanElement nameSpan;

    public HelloWorld() {
        setElement(uiBinder.createAndBindUi(this));
    }

    public void setName(String name) { 
        nameSpan.setInnerText(name); 
    }

    /**
    * Method in question
    */
    public void Element getElement() { 
        return nameSpan; 
    }
}

getElement() 方法具有 void 返回类型,但返回名为 nameSpanElement。鉴于它具有 void 返回类型,这怎么可能?

最佳答案

解释很简单,文档中的示例“有点”损坏。

如果示例只是扩展了 UIObject,那么 setElement()getElement() 的实现就没有必要了

public class HelloWorld extends UIObject {

  private static HelloWorldUiBinder uiBinder = 
        GWT.create(HelloWorldUiBinder.class);

  interface HelloWorldUiBinder extends UiBinder<Element, HelloWorld> {
  }

  @UiField
  SpanElement nameSpan;

  public HelloWorld() {
    setElement(uiBinder.createAndBindUi(this));
  }

  public void setName(String name) { 
    nameSpan.setInnerText(name); 
  }

}
<小时/>

顺便说一句,这是 UiBinder“hello world”示例的独立变体(作为第一个 UiBinder 示例可能更容易理解):

public class HelloWorld implements EntryPoint {

  interface HelloWorldUiBinder extends UiBinder<Element, HelloWorld> {
  }

  @UiField SpanElement nameSpan;

  public void onModuleLoad() {
    final HelloWorldUiBinder uiBinder = GWT.create(HelloWorldUiBinder.class);
    final Element element = uiBinder.createAndBindUi(this);
    nameSpan.setInnerText("world");
    Document.get().getBody().appendChild(element);
  }
}

关于java - GWT UIBinder 方法返回类型 void?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12122684/

相关文章:

java - 判断字符串是否为数字的函数会引发异常

java - Android 编程时 Eclipse 出现空指针异常

java - Spring + GWT + jackson + JSON响应文本似乎不是标准响应格式

google-app-engine - Google 应用引擎的智能 GWT 或 ext GWT 或 Vaadin?

java - GWT - 如何更改复选框属性

java - 从 Java 对象类到 C++

java - 列出不同用户的 Google 日历 Activity

gwt - GET声明式布局父属性

java - 如何将 @UiHandler 与自定义处理程序一起使用(传递构造函数参数)

facebook - 你如何让 UiBinder 传递标签而不绑定(bind)它们以便 XFBML 工作?