- GWT 教程
- GWT - 主页
- GWT - 概述
- GWT - 环境设置
- GWT - 应用
- GWT - 创建应用程序
- GWT - 部署应用程序
- GWT - 使用 CSS 的样式
- GWT - 基本小部件
- GWT - 表单小部件
- GWT - 复杂的小部件
- GWT - 布局面板
- GWT - 事件处理
- GWT - 自定义小部件
- GWT-UIBinder
- GWT - RPC 通信
- GWT - JUnit 集成
- GWT - 调试应用程序
- GWT-- 国际化
- GWT - 历史课
- GWT - 书签支持
- GWT - 日志框架
- GWT 有用资源
- GWT - 问题与解答
- GWT - 快速指南
- GWT - 有用的资源
- GWT - 讨论
GWT - 事件处理
GWT 提供了类似于 Java AWT 或 SWING 用户界面框架的事件处理程序模型。
侦听器接口定义小部件调用以宣布事件的一个或多个方法。GWT 提供了与各种可能的事件相对应的接口列表。
希望接收特定类型事件的类实现关联的处理程序接口,然后将对其自身的引用传递给小部件以订阅一组事件。
例如,Button类发布单击事件,因此您必须编写一个类来实现ClickHandler来处理单击事件。
事件处理程序接口
所有 GWT 事件处理程序都从EventHandler接口扩展,每个处理程序只有一个带有单个参数的方法。该参数始终是关联事件类型的对象。每个事件对象都有许多方法来操作传递的事件对象。例如,对于单击事件,您必须按如下方式编写处理程序 -
/**
* create a custom click handler which will call
* onClick method when button is clicked.
*/
public class MyClickHandler implements ClickHandler {
@Override
public void onClick(ClickEvent event) {
Window.alert("Hello World!");
}
}
现在,任何希望接收单击事件的类都将调用addClickHandler()来注册事件处理程序,如下所示 -
/**
* create button and attach click handler
*/
Button button = new Button("Click Me!");
button.addClickHandler(new MyClickHandler());
每个支持事件类型的小部件都将具有 HandlerRegistration add Foo Handler( Foo Event)形式的方法,其中Foo是实际事件,如 Click、Error、KeyPress 等。
以下是重要的 GWT 事件处理程序以及关联事件和处理程序注册方法的列表 -
| 先生。 | 事件接口 | 活动方式及说明 |
|---|---|---|
| 1 | 选择处理程序<I>之前 |
在选择之前无效(选择之前事件<I> 事件); 在触发 BeforeSelectionEvent 时调用。 |
| 2 | 模糊处理器 |
模糊无效(模糊事件事件); 当模糊事件被触发时调用。 |
| 3 | 变更处理器 |
更改无效(ChangeEvent 事件); 当触发更改事件时调用。 |
| 4 | 点击处理程序 |
单击无效(ClickEvent 事件); 当触发本机单击事件时调用。 |
| 5 | 关闭处理程序<T> |
关闭时无效(CloseEvent<T> 事件); 当 CloseEvent 被触发时调用。 |
| 6 | 上下文菜单处理程序 |
上下文菜单上无效(上下文菜单事件事件); 当触发本机上下文菜单事件时调用。 |
| 7 | 双击处理程序 |
双击无效(双击事件事件); 当双击事件被触发时调用。 |
| 8 | 错误处理程序 |
错误时无效(错误事件事件); 当错误事件被触发时调用。 |
| 9 | 焦点处理程序 |
焦点无效(焦点事件事件); 当焦点事件被触发时调用。 |
| 10 | 表单面板.提交完整的处理程序 |
提交完成时无效(表单面板.提交完成事件事件); 表单提交成功后触发。 |
| 11 | FormPanel.SubmitHandler |
提交时无效(表单面板.提交事件事件); 提交表单时触发。 |
| 12 | 按键按下处理程序 |
按键按下时无效(按键按下事件事件); 当 KeyDownEvent 被触发时调用。 |
| 13 | 按键处理程序 |
KeyPress 上无效(KeyPressEvent 事件); 当 KeyPressEvent 被触发时调用。 |
| 14 | 按键处理程序 |
void on KeyUp(KeyUpEvent 事件); 当 KeyUpEvent 被触发时调用。 |
| 15 | 加载处理程序 |
加载时无效(LoadEvent 事件); 当 LoadEvent 被触发时调用。 |
| 16 | 鼠标按下处理程序 |
MouseDown 上无效(MouseDownEvent 事件); 当 MouseDown 被触发时调用。 |
| 17 号 | 鼠标移动处理程序 |
MouseMove 上无效(MouseMoveEvent 事件); 当 MouseMoveEvent 被触发时调用。 |
| 18 | 鼠标输出处理程序 |
MouseOut 上无效(MouseOutEvent 事件); 当 MouseOutEvent 被触发时调用。 |
| 19 | 鼠标悬停处理程序 |
MouseOver 上无效(MouseOverEvent 事件); 当 MouseOverEvent 被触发时调用。 |
| 20 | 鼠标按下处理程序 |
MouseUp 上无效(MouseUpEvent 事件); 当 MouseUpEvent 被触发时调用。 |
| 21 | 鼠标滚轮处理程序 |
MouseWheel 上无效(MouseWheelEvent 事件); 当 MouseWheelEvent 被触发时调用。 |
| 22 | 调整大小处理程序 |
调整大小时无效(ResizeEvent 事件); 当小部件调整大小时触发。 |
| 23 | 滚动处理程序 |
滚动无效(ScrollEvent 事件); 当 ScrollEvent 被触发时调用。 |
| 24 | 选择处理程序<I> |
选择无效(SelectionEvent<I> 事件); 当 SelectionEvent 被触发时调用。 |
| 25 | ValueChangeHandler<I> |
void on ValueChange(ValueChangeEvent<I> 事件); 当 ValueChangeEvent 被触发时调用。 |
| 26 | Window.ClosingHandler |
WindowClosing 上无效(Window.ClosingEvent 事件); 在浏览器窗口关闭或导航到其他站点之前触发。 |
| 27 | Window.ScrollHandler |
void on WindowScroll(Window.ScrollEvent 事件); 当浏览器窗口滚动时触发。 |
事件方法
如前所述,每个处理程序都有一个带有单个参数的方法,用于保存事件对象,例如void onClick(ClickEvent event)或void onKeyDown(KeyDownEvent event)。ClickEvent和KeyDownEvent等事件对象有一些常用方法,如下所示 -
| 先生。 | 方法及说明 |
|---|---|
| 1 |
protected void dispatch(ClickHandler handler)此方法只能由 HandlerManager 调用 |
| 2 |
DomEvent.Type <FooHandler> getAssociatedType()此方法返回用于注册Foo事件的类型。 |
| 3 |
static DomEvent.Type<FooHandler> getType()此方法获取与Foo事件关联的事件类型。 |
| 4 |
public java.lang.Object getSource()此方法返回最后触发此事件的源。 |
| 5 |
protected final boolean isLive()此方法返回事件是否处于活动状态。 |
| 6 |
protected void Kill()该方法终止事件 |
例子
此示例将引导您完成简单的步骤,展示GWT 中Click事件和KeyDown事件处理的用法。按照以下步骤更新我们在GWT - 创建应用程序章节中创建的 GWT 应用程序 -
| 步 | 描述 |
|---|---|
| 1 | 按照GWT - 创建应用程序章节中的说明,在com.tutorialspoint包下创建一个名为HelloWorld的项目。 |
| 2 | 如下所述修改HelloWorld.gwt.xml、HelloWorld.css、HelloWorld.html和HelloWorld.java 。保持其余文件不变。 |
| 3 | 编译并运行应用程序以验证实现逻辑的结果。 |
以下是修改后的模块描述符src/com.tutorialspoint/HelloWorld.gwt.xml的内容。
<?xml version = "1.0" encoding = "UTF-8"?> <module rename-to = 'helloworld'> <!-- Inherit the core Web Toolkit stuff. --> <inherits name = 'com.google.gwt.user.User'/> <!-- Inherit the default GWT style sheet. --> <inherits name = 'com.google.gwt.user.theme.clean.Clean'/> <!-- Specify the app entry point class. --> <entry-point class = 'com.tutorialspoint.client.HelloWorld'/> <!-- Specify the paths for translatable code --> <source path = 'client'/> <source path = 'shared'/> </module>
以下是修改后的样式表文件war/HelloWorld.css的内容。
body {
text-align: center;
font-family: verdana, sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
color: #777777;
margin: 40px 0px 70px;
text-align: center;
}
以下是修改后的 HTML 主机文件war/HelloWorld.html的内容。
<html>
<head>
<title>Hello World</title>
<link rel = "stylesheet" href = "HelloWorld.css"/>
<script language = "javascript" src = "helloworld/helloworld.nocache.js">
</script>
</head>
<body>
<h1>Event Handling Demonstration</h1>
<div id = "gwtContainer"></div>
</body>
</html>
让我们了解 Java 文件src/com.tutorialspoint/HelloWorld.java的以下内容,它将演示 GWT 中事件处理的使用。
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
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;
public class HelloWorld implements EntryPoint {
public void onModuleLoad() {
/**
* create textbox and attach key down handler
*/
TextBox textBox = new TextBox();
textBox.addKeyDownHandler(new MyKeyDownHandler());
/*
* create button and attach click handler
*/
Button button = new Button("Click Me!");
button.addClickHandler(new MyClickHandler());
VerticalPanel panel = new VerticalPanel();
panel.setSpacing(10);
panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
panel.setSize("300", "100");
panel.add(textBox);
panel.add(button);
DecoratorPanel decoratorPanel = new DecoratorPanel();
decoratorPanel.add(panel);
RootPanel.get("gwtContainer").add(decoratorPanel);
}
/**
* create a custom click handler which will call
* onClick method when button is clicked.
*/
private class MyClickHandler implements ClickHandler {
@Override
public void onClick(ClickEvent event) {
Window.alert("Hello World!");
}
}
/**
* create a custom key down handler which will call
* onKeyDown method when a key is down in textbox.
*/
private class MyKeyDownHandler implements KeyDownHandler {
@Override
public void onKeyDown(KeyDownEvent event) {
if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER){
Window.alert(((TextBox)event.getSource()).getValue());
}
}
}
}
准备好完成所有更改后,让我们在开发模式下编译并运行应用程序,就像我们在GWT - 创建应用程序章节中所做的那样。如果您的应用程序一切正常,这将产生以下结果 -
