首先,下载GWT,http://code.google.com/webtoolkit/;
运行->cmd,创建一个工程文件夹:C:/> mkdir MyProject;
设置PATH路径:C:/>set path=%JAVA_HOME%/bin;%GWT_HOME%;
applicationCreator.cmd 命令支持的语法如下:
ApplicationCreator [-eclipse projectName] [-out dir] [-overwrite] [-ignore] className
进入MyProject目录,执行projectCreator -eclipse MyProject;
继续执行applicationCreator -eclipse MyProject com.mycompany.client.MyApplication;
此时,执行MyApplication-shell将会看到Google Web Toolkit Development Shell和Wrapper HTML for MyApplication两个Google界面,并且在后者有一个简答的按钮测试;执行MyApplication-compile将进行编译;
启动Eclipse,导入现有工程,选择MyProject,即可看到这个GWT应用的完整结构目录:)
其中com.mycompany.client.MyApplication类,就是页面布局的程序,我把它改了改,加了一个
Frame,加了一个类似注册用户输入用户名后提示用户名是否被占用的效果,代码如下:
package com.mycompany.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.*;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class MyApplication implements EntryPoint {
/**
* This is the entry point method.
*/
public void onModuleLoad() {
final Button button = new Button("Click me");
final Label label = new Label();
final Label label1 = new Label();
// Make a new frame, and point it at Google.
Frame frame = new Frame("http://www.google.com/");
// Add it to the root panel.
RootPanel.get().add(frame);
final TextBox tb = new TextBox();
tb.addFocusListener(new FocusListener(){
public void onFocus(Widget sender){
}
public void onLostFocus(Widget sender){
if (tb.getText().equals("ryan"))
label1.setText("Welcome!");
else
label1.setText("Error!");
}
});
RootPanel.get().add(tb);
RootPanel.get().add(label1);
button.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
if (label.getText().equals(""))
{ label.setText("Hello World!");
Window.alert("AJAX");
}
else
label.setText("");
}
});
// Assume that the host HTML has elements defined whose
// IDs are "slot1", "slot2". In a real app, you probably would not want
// to hard-code IDs. Instead, you could, for example, search for all
// elements with a particular CSS class and replace them with widgets.
//
RootPanel.get("slot1").add(button);
RootPanel.get("slot2").add(label);
}
}
修改完后,执行MyApplication-compile编译后,在IE中测试MyApplication.html,
或者执行MyApplication-shell,用GWT自带的调试器看效果也可以。
总之,GWT用纯java的方式实现了丰富的AJAX效果,GWT包含了HTML的各种组件,用java
传统的事件处理机制,对java熟悉的人可以很快上手。
GWT快速入门
本文介绍如何使用Google Web Toolkit (GWT) 构建基于Java的富互联网应用程序。从下载GWT到创建并编译第一个项目,再到实现简单的AJAX效果,提供了一步步的指导。
4329

被折叠的 条评论
为什么被折叠?



