2011-01-29 186 views
4

我目前正在嘗試使用GWT的UIBinder功能,但沒有成功。我已經多次閱讀文檔,但並未包含完整的文章。也許你可以幫助我。GWT:在HTML模板中使用UIBinder ...如何讓它工作?

以下是我目前所知:

1)我需要創建例如一個帶有一些小部件佈局的HelloWorld.ui.xml文件。 2)我需要創建一個相應的HelloWorld類。

這是我無法找到有關信息:

A)我在哪裏把HelloWorld.ui.xml文件,使GWT找到它?

B)如何將我的HelloWorld組件添加到一個小組?

該文檔非常稀缺,並且由已經瞭解GWT的人知道,一個新手不知道的人肯定會寫。

+0

另外,安裝[Google Plugin for Eclipse](http://code.google.com/eclipse/),它將處理大部分工作(您只需放置組件名稱,它將創建相應的java和ui.xml文件).... [其他事項](http://code.google.com/eclipse/docs/gwt.html):) – 2011-01-29 13:19:45

回答

2

A)您需要將HelloWorld.ui.xml文件放在與包含該ui.xml文件的邏輯的小部件類相同的包中。類名應該是HelloWorld(爲了簡單起見,我說你需要使用相同的名稱,但可以通過代碼爲ui.xml文件使用不同的名稱)。

B)您的HelloWorld類應該是一個擴展小部件的類。就像任何'普通'小部件一樣,它可以添加到任何面板。

這裏是綁定HelloWorld.ui.xml在你HelloWorld控件類的代碼:

public class HelloWorld extends Composite /*or extend any widget you want*/ { 
    //This defines an interface that represents this specific HelloWorld.ui.xml file. 
    interface MyUiBinder extends UiBinder<Widget, HelloWorld> {} 

    // This code is for GWT so it can generate the code from your HelloWorld.ui.xml 
    private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class); 

    //Constructor 
    public HelloWidgetWorld() { 
     // This binds the HelloWorld.ui.xml with this widget 
     initWidget(uiBinder.createAndBindUi(this)); 
     ... 
    } 

    ... 
} 
2

本概述說明了一切:http://code.google.com/webtoolkit/doc/latest/DevGuideUiBinder.html

  1. 你把HelloWorld.ui.xml在同一文件夾中你的HelloWorld.java文件。
  2. 喜歡這張(取自上面的鏈接):

    public class HelloWorld extends UIObject { // Could extend Widget instead 
        interface HelloWorldUiBinder extends UiBinder<DivElement, HelloWorld> {} 
        private static HelloWorldUiBinder uiBinder = 
          GWT.create(HelloWorldUiBinder.class); 
    
        // div element created via UiBinder 
        private DivElement divElement; 
    
        public HelloWorld() { 
         // createAndBindUi 
         divElement = uiBinder.createAndBindUi(this); 
         // now you can add created DivElement to your panel 
        } 
    }; 
    
1

這裏有兩個完整的教程和HelloWorld的樣品:

1),用於UI粘結劑與GWT控制: http://blog.jeffdouglas.com/2010/01/19/gwt-uibinder-hello-world-tutorial/

2)使用純HTML的UI活頁夾: 糟糕! StackOverflow不允許我發佈兩個超鏈接。因此,我將在單獨的答案中發佈第二個答案,或者在此評論中發佈第二個答案。

+0

下面是純HTML樣本的鏈接:http: //blog.iparissa.com/gwt-uibinder-helloworld-with-html/ – Esfand 2011-01-29 18:34:47