gwt的這些特性還是很有意思的,感覺比echo更貼近html, 比如說尺寸等數(shù)據(jù),寫"20%"和"200px"都可以。echo則盡量封裝的象swing, 屏蔽掉html.
anyway, 對于大多數(shù)邏輯都在客戶端的應(yīng)用,gwt可以大展身手。比如小游戲~
guess number demo看這里:http://steeven.googlepages.com/MyApp.html
完全在瀏覽器上運(yùn)行的玩意,沒有寫一句js,感覺還是很爽的~
代碼如下:
package?org.steeven.gwt.test.client;

import?com.google.gwt.core.client.EntryPoint;
import?com.google.gwt.user.client.Random;
import?com.google.gwt.user.client.ui.Button;
import?com.google.gwt.user.client.ui.ClickListener;
import?com.google.gwt.user.client.ui.DialogBox;
import?com.google.gwt.user.client.ui.Grid;
import?com.google.gwt.user.client.ui.HasHorizontalAlignment;
import?com.google.gwt.user.client.ui.RootPanel;
import?com.google.gwt.user.client.ui.TextBox;
import?com.google.gwt.user.client.ui.VerticalPanel;
import?com.google.gwt.user.client.ui.Widget;


/**?*//**
?*?@author?steeven@gmail.com
?*/

public?class?MyApp?implements?EntryPoint?
{

????TextBox?txtCount?=?new?TextBox();

????private?Grid?pnlMain;

????private?Button[]?numbers?=?new?Button[100];

????private?int?target;

????private?int?count;

????private?DialogBox?box;

????private?Button?btnRetry;

????private?Button?btnClose;


????/**?*//**
?????*?This?is?the?entry?point?method.
?????*/

????public?void?onModuleLoad()?
{
????????VerticalPanel?pnlStatus?=?new?VerticalPanel();
????????pnlStatus.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
????????pnlStatus.setSpacing(20);

????????txtCount.setEnabled(false);
????????txtCount.setVisibleLength(10);
????????pnlStatus.add(txtCount);
????????btnRetry?=?new?Button();
????????btnRetry.setHTML("<img?src=\"replay.gif\"/>?<u>R</u>etry");
????????btnRetry.setAccessKey('r');

????????btnRetry.addClickListener(new?ClickListener()?
{

????????????public?void?onClick(Widget?sender)?
{
????????????????doInit();
????????????}
????????});
????????pnlStatus.add(btnRetry);

????????Button?btnAbout?=?new?Button();
????????btnAbout.setHTML("<img?src='about.gif'/>?<u>A</u>bout");
????????btnAbout.setAccessKey('a');

????????btnAbout.addClickListener(new?ClickListener()?
{

????????????public?void?onClick(Widget?sender)?
{
????????????????doAbout();
????????????}
????????});
????????pnlStatus.add(btnAbout);

????????RootPanel.get("status").add(pnlStatus);

????????pnlMain?=?new?Grid(10,?10);
????????RootPanel.get("main").add(pnlMain);


????????for?(int?i?=?0;?i?<?100;?i++)?
{
????????????numbers[i]?=?new?Button();
????????????numbers[i].setText(i?+?"");

????????????numbers[i].addClickListener(new?ClickListener()?
{

????????????????public?void?onClick(Widget?sender)?
{
????????????????????doGuess(sender);
????????????????}
????????????});
????????????pnlMain.setWidget(i?/?10,?i?%?10,?numbers[i]);
????????}

????????box?=?new?DialogBox();
????????box.setPopupPosition(400,?200);

????????btnClose?=?new?Button("<u>C</u>lose",?new?ClickListener()?
{

????????????public?void?onClick(Widget?sender)?
{
????????????????box.hide();
????????????????doInit();
????????????}
????????});
????????btnClose.setAccessKey('c');
????????box.add(btnClose);
????????doInit();
????}


????protected?void?doGuess(Widget?sender)?
{
????????Button?btn?=?(Button)?sender;
????????btnRetry.setEnabled(true);
????????int?n?=?Integer.parseInt(btn.getText());
????????txtCount.setText(""?+?(++count));

????????if?(n?==?target)?
{
????????????numbers[n].setEnabled(false);
????????????btnClose.setFocus(true);
????????????box.clear();
????????????box
????????????????????.setHTML("<center><img?src='win.gif'/><h1>YOU?WIN!!!</h1><br/><br/><br/>");
????????????box.add(btnClose);
????????????box.show();

????????}?else?
{
????????????if?(n?<?target)
????????????????for?(int?i?=?0;?i?<=?n;?i++)
????????????????????numbers[i].setEnabled(false);
????????????else
????????????????for?(int?i?=?n;?i?<?100;?i++)
????????????????????numbers[i].setEnabled(false);
????????}

????}


????protected?void?doAbout()?
{
????????box.clear();
????????box
????????????????.setHTML("<img?src='about.gif'/><h1>Guess?Number</h1><h3>Google?web?toolkit?test</h3>");
????????box.add(btnClose);
????????box.show();
????}


????private?void?doInit()?
{
????????btnRetry.setEnabled(false);
????????target?=?Random.nextInt(99);
????????count?=?0;
????????txtCount.setText("0");

????????for?(int?i?=?0;?i?<?100;?i++)?
{
????????????numbers[i].setVisible(true);
????????????numbers[i].setEnabled(true);
????????}
????}

}

第一次玩gwt, 總共花了3個(gè)小時(shí),菜呀
anyway, 對于大多數(shù)邏輯都在客戶端的應(yīng)用,gwt可以大展身手。比如小游戲~
guess number demo看這里:http://steeven.googlepages.com/MyApp.html
完全在瀏覽器上運(yùn)行的玩意,沒有寫一句js,感覺還是很爽的~
代碼如下:
















































































































































































第一次玩gwt, 總共花了3個(gè)小時(shí),菜呀
最差的是8次,理論上說應(yīng)該7次就夠了吧?
http://code.google.com/webtoolkit/gettingstarted.html
1. 建立目錄,進(jìn)去。md test; cd test. (后面兩個(gè)批處理都在當(dāng)前目錄下干活)
2. 先運(yùn)行..\projectCreator.cmd創(chuàng)建項(xiàng)目(--help查看幫助)
3. 然后運(yùn)行..\applicationCreator.cmd
4. 在eclipse里面import, 選擇existing project即可。
host mode debug話要引入那個(gè)巨大的jar(最好在eclipse里面建一個(gè)變量,以后新建項(xiàng)目比較方便)。引入以后找到com.google.gwt.dev.GWTShell,當(dāng)普通java類調(diào)試即可。
這里可能也是GWK聰明的地方,ECHO里面這些東西一般要用控件組成.GWT則允許HTML的直接存在.控件多了不利于效率的提高.
gwt現(xiàn)在還沒有rpc的sample, 在group里面找到一個(gè)。上次好像有人問過。
[ERROR] Unable to load module entry point class com.sample.guess.client.GuessNum
java.lang.NullPointerException: null
at com.sample.guess.client.GuessNum.onModuleLoad(GuessNum.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:138)
[ERROR] Failure to load module 'com.sample.guess.GuessNum'