GWT提供类似于Java AWT或SWING用户界面框架的事件处理程序模型.
侦听器接口定义一个或者小部件调用以宣告事件的更多方法. GWT提供了与各种可能事件对应的接口列表.
希望接收特定类型事件的类实现关联的处理程序接口,然后传递引用自己到窗口小部件订阅一组事件.
例如, Button 类发布点击活动所以您必须编写一个类来实现 ClickHandler 来处理点击事件.
所有GWT事件处理程序都是从 EventHandler 接口扩展而来的,每个处理程序只有一个方法只有一个论点.此参数始终是关联事件类型的对象.每个事件对象都有许多方法来操作传递的事件对象.例如,对于click事件,您将必须编写处理程序,如下所示 :
/** * 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 事件)其中 Foo 是Click,Error,KeyPress等实际事件.
以下是重要的GWT事件处理程序及相关事件和处理程序注册的列表方法 :
Sr.No. | 事件接口 | 事件方法&说明 |
---|---|---|
1 | Before Selection Handler< I> | 在选择之前无效(选择事件之前< I>事件); 在触发BeforeSelectionEvent时调用. |
2 | BlurHandler | Blur上的void(模糊事件事件); 当Blur事件被触发时调用. |
3 | ChangeHandler | 变更无效(ChangeEvent事件) ); 触发更改事件时调用. |
4 | ClickHandler | Click on Click(ClickEvent事件); 触发本机点击事件时调用. |
5 | CloseHandler< T> | 关闭时无效(CloseEvent< T>事件); 在触发CloseEvent时调用. |
6 | Context Menu Handler | 上下文菜单上的void(上下文菜单事件事件); 在触发本机上下文菜单事件时调用. |
7 | Double Click Handler | 双击(双击事件事件)无效; 双人时调用点击事件被触发. |
8 | Error Handler | 错误时出现错误(错误事件事件); 触发错误事件时调用. |
9 | Focus Handler | 无效焦点(焦点事件)事件); 在焦点事件被触发时调用. |
10 | Form Panel.Submit Complete Handler | 提交完成时无效(表单面板.提交完成事件事件); 成功提交表单时触发. |
11 | FormPanel.SubmitHandler | 提交时无效(表单Panel.Submit事件事件); 表单时触发提交. |
12 | Key Down Handler | 按键按下时无效(按键事件事件); 触发KeyDownEvent时调用. |
13 | KeyPressHandler | 在KeyPress上无效(KeyPressEvent事件); 在触发KeyPressEvent时调用. |
14 | KeyUpHandler | 在KeyUp上无效(KeyUpEvent事件); 在触发KeyUpEvent时调用. |
15 | LoadHandler | 加载时无效(LoadEvent事件); 在触发LoadEvent时调用. |
16 | MouseDownHandler | 在MouseDown上无效(MouseDownEvent事件); 在MouseDown被触发时调用. |
17 | MouseMoveHandler | 无效MouseMove(MouseMoveEvent事件); 触发MouseMoveEvent时调用. |
18 | MouseOutHandler | 在MouseOut上无效(MouseOutEvent事件); 在触发MouseOutEvent时调用. |
19 | MouseOverHandler | 在MouseOver上无效(MouseOverEvent事件); 在触发MouseOverEvent时调用. |
20 | MouseUpHandler | 在MouseUp上无效(MouseUpEvent事件); 在MouseUpEvent时调用被解雇. |
21 | MouseWheelHandler | 在MouseWheel上无效(MouseWheelEvent事件); 在触发MouseWheelEvent时调用. |
22 | ResizeHandler | 调整大小时无效(ResizeEvent事件); 调整小部件大小时触发. |
23 | ScrollHandler | 滚动时无效(滚动事件事件); 触发ScrollEvent时调用. |
24 | SelectionHandler< I> | 选择时 无效(SelectionEvent< I>事件); 当LaserEvent被触发时调用. |
25 | ValueChangeHandler< I> | 在ValueChange上为void(ValueChangeEvent< I>事件); 在触发ValueChangeEvent时调用. |
26 | Window.ClosingHandler | WindowClosing上的void(Window.ClosingEvent事件); 在浏览器窗口关闭之前触发或导航到另一个站点. |
27 | Window.ScrollHandler | 在WindowScroll上无效(Window.ScrollEvent事件); 滚动浏览器窗口时触发. |
如前所述,每个处理程序都有一个方法,只有一个参数保存事件ob ject,例如 void onClick(ClickEvent事件)或 void onKeyDown(KeyDownEvent事件).像 ClickEvent 和 KeyDownEvent 这样的事件对象有几个常见的方法,列在下面 :
Sr.No. | Method&描述 |
---|---|
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() this method返回上次触发此事件的源. |
5 | protected final boolean isLive()此方法返回事件是否有效. |
6 | protected void kill()此方法终止事件 |
此示例将指导您完成在GWT中显示单击事件和 KeyDown 事件处理的使用的简单步骤.按照以下步骤更新我们在 GWT - 创建应用程序章节中创建的GWT应用程序;
步骤 | 描述 |
---|---|
1 | 在包 com.it1352下创建一个名为 HelloWorld 的项目,如下所述在 GWT - 创建应用程序章节. |
2 | 修改 HelloWorld.gwt.xml , HelloWorld.css , HelloWorld.html 和 HelloWorld.java 如下所述.保持其余文件不变. |
3 | 编译并运行应用程序以验证实现的逻辑的结果. |
以下是修改后的模块描述符的内容 src/com.it1352/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.IT屋.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.it1352/HelloWorld.java 的内容,它将演示事件处理的使用在GWT.
package com.it1352.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 - 创建应用程序一章中那样在开发模式下编译和运行应用程序.如果您的应用程序一切正常,这将产生以下结果 :