当前位置: 首页 > 教程 > GWT >

GWT 事件处理

精华
小牛编辑
177浏览
2023-03-14

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 事件处理程序和相关事件和处理程序注册方法的列表:

事件接口 事件方法和描述
Before Selection Handler<I>

void on Before Selection (Before Selection Event<I> event);
在触发 BeforeSelectionEvent 时调用。

BlurHandler void on Blur(Blur Event event);
当模糊事件被触发时调用。
ChangeHandler void on Change(ChangeEvent event);
触发更改事件时调用。
ClickHandler void on Click(ClickEvent event);
当本机点击事件被触发时调用。
CloseHandler<T> void on Close(CloseEvent<T> event);
当 CloseEvent 被触发时调用。
Context Menu Handler void on Context Menu(Context Menu Event event);
当本机上下文菜单事件被触发时调用。
Double Click Handler void on Double Click(Double Click Event event);
在触发双击事件时调用。
Error Handler void on Error(Error Event event);
触发错误事件时调用。
Focus Handler void on Focus(Focus Event event);
当焦点事件被触发时调用。
Form Panel.Submit Complete Handler void on Submit Complete(Form Panel.Submit Complete Event event);
当表单成功提交时触发。
FormPanel.SubmitHandler void on Submit(Form Panel.Submit Event event);
提交表单时触发。
Key Down Handler void on Key Down(Key Down Event event);
当 KeyDownEvent 被触发时调用。
KeyPressHandler void on KeyPress(KeyPressEvent event);
当 KeyPressEvent 被触发时调用。
KeyUpHandler void on KeyUp(KeyUpEvent event);
当 KeyUpEvent 被触发时调用。
LoadHandler void on Load(LoadEvent event);
当 LoadEvent 被触发时调用。
MouseDownHandler void on MouseDown(MouseDownEvent event);
当 MouseDown 被触发时调用。
MouseMoveHandler void on MouseMove(MouseMoveEvent event);
当 MouseMoveEvent 被触发时调用。
MouseOutHandler void on MouseOut(MouseOutEvent event);
当 MouseOutEvent 被触发时调用。
MouseOverHandler void on MouseOver(MouseOverEvent event);
当 MouseOverEvent 被触发时调用。
MouseUpHandler void on MouseUp(MouseUpEvent event);
当 MouseUpEvent 被触发时调用。
MouseWheelHandler void on MouseWheel(MouseWheelEvent event);
当 MouseWheelEvent 被触发时调用。
ResizeHandler void on Resize(ResizeEvent event);
当小部件调整大小时触发。
ScrollHandler void on Scroll(ScrollEvent event);
当 ScrollEvent 被触发时调用。
SelectionHandler<I> void on Selection(SelectionEvent<I> event);
当 SelectionEvent 被触发时调用。
ValueChangeHandler<I> void on ValueChange(ValueChangeEvent<I> event);
当 ValueChangeEvent 被触发时调用。
Window.ClosingHandler void on WindowClosing(Window.ClosingEvent event);
在浏览器窗口关闭或导航到不同站点之前触发。
Window.ScrollHandler void on WindowScroll(Window.ScrollEvent event);
当浏览器窗口滚动时触发。

事件方法

如前所述,每个处理程序都有一个带有单个参数的方法,该方法保存事件对象,例如void onClick(ClickEvent event)或void onKeyDown(KeyDownEvent event)。ClickEvent和KeyDownEvent等事件对象有几个常用方法,如下所示:

方法 描述
protected void dispatch(ClickHandler handler) 这个方法只能被HandlerManager调用
DomEvent.Type <FooHandler> getAssociatedType() 该方法返回用于注册Foo事件的类型。
static DomEvent.Type<FooHandler> getType() 该方法获取与Foo事件关联的事件类型。
public java.lang.Object getSource() 这个方法返回最后触发这个事件的源。
protected final boolean isLive() 这个方法返回事件是否是实时的。
protected void kill() 这个方法kill事件

GWT 事件处理 示例

1)修改HelloWorld.gwt.xml

<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.8.0//EN"
        "http://gwtproject.org/doctype/2.8.0/gwt-module.dtd">
<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='cn.xnip.helloWorld.client.HelloWorld'/>


    <!-- Specify the app servlets.                   -->
    <servlet path='/HelloWorldService' class='cn.xnip.helloWorld.server.HelloWorldServiceImpl'/>

    <source path = 'client'/>
    <source path = 'shared'/>
</module>

2)修改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;
}

3)修改HelloWorld.html

<html>
<head>
    <title>xnip.cn-GWT Hello World</title>
    <link type="text/css" rel="stylesheet" href="HelloWorld.css">
    <script type="text/javascript" language="javascript" src="HelloWorld/HelloWorld.nocache.js"></script>
</head>
<body>
<h1>Event Handling Demonstration</h1>
<div id = "gwtContainer"></div>
</body>
</html>

4)HelloWorld.java

package cn.xnip.helloWorld.client;


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;

/**
 * Entry point classes define <code>onModuleLoad()</code>
 */
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());
            }
        }
    }
}

运行应用程序,显示结果如下: