面向 Java 开发人员的 Ajax: 探索 Google Web Toolkit
http://tech.ddvip.com 2006年11月20日 社区交流
本文详细介绍面向 Java 开发人员的 Ajax: 探索 Google Web Toolkit
所有小部件和面板最终都要附加到包含它们的 Web 页面上。如 清单 1 所示,可以直接把它们附加到RootPanel上。或者,可以用RootPanel获得对使用 ID 或类名标识的 HTML 元素的引用。在这个示例中,我将使用两个独立的 HTMLDIV元素,它们的名称分别是input-container和output-container。第一个元素包含 Weather Reporter 应用程序的 UI 控件,第二个元素显示天气报告本身。
清单 2 显示了设置基本布局所需的代码;它应当是自解释的。HTML小部件只是 HTML 标记的容器,来自 Yahoo! 天气种子(weather feed)的 HTML 输出将显示在这里。这些代码都位于Weather类的onModuleLoad()方法中,这个方法由EntryPoint接口提供。在将包含天气模块的 Web 页面装入客户机的 Web 浏览器时,将调用这个方法。
清单 2. Weather Reporter 应用程序的布局代码
public void onModuleLoad() {
HorizontalPanel inputPanel = new HorizontalPanel();
// Align child widgets along middle of panel
inputPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
Label lbl = new Label("5-digit zipcode: ");
inputPanel.add(lbl);
TextBox txBox = new TextBox();
txBox.setVisibleLength(20);
inputPanel.add(txBox);
// Create radio button group to select units in C or F
Panel radioPanel = new VerticalPanel();
RadioButton ucRadio = new RadioButton("units", "Celsius");
RadioButton ufRadio = new RadioButton("units", "Fahrenheit");
// Default to Celsius
ucRadio.setChecked(true);
radioPanel.add(ucRadio);
radioPanel.add(ufRadio);
// Add radio buttons panel to inputs
inputPanel.add(radioPanel);
// Create Submit button
Button btn = new Button("Submit");
// Add button to inputs, aligned to bottom
inputPanel.add(btn);
inputPanel.setCellVerticalAlignment(btn,
HasVerticalAlignment.ALIGN_BOTTOM);
RootPanel.get("input-container").add(inputPanel);
// Create widget for HTML output
HTML weatherHtml = new HTML();
RootPanel.get("output-container").add(weatherHtml);
}图 2 显示了在 GWT Shell 中呈现的布局:
来源:ibm 作者:Philip McCarthy 责编:豆豆技术应用
正在加载评论...