📜  GWT-事件处理

📅  最后修改于: 2020-10-25 04:41:49             🧑  作者: Mango


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 Event)形式的方法,其中Foo是实际事件,例如Click,Error,KeyPress等。

以下是重要的GWT事件处理程序以及相关事件和处理程序注册方法的列表-

Sr.No. Event Interface Event Method & Description
1 Before Selection Handler

void on Before Selection (Before Selection Event event);

Called when BeforeSelectionEvent is fired.

2 BlurHandler

void on Blur(Blur Event event);

Called when Blur Event is fired.

3 ChangeHandler

void on Change(ChangeEvent event);

Called when a change event is fired.

4 ClickHandler

void on Click(ClickEvent event);

Called when a native click event is fired.

5 CloseHandler

void on Close(CloseEvent event);

Called when CloseEvent is fired.

6 Context Menu Handler

void on Context Menu(Context Menu Event event);

Called when a native context menu event is fired.

7 Double Click Handler

void on Double Click(Double Click Event event);

Called when a Double Click Event is fired.

8 Error Handler

void on Error(Error Event event);

Called when Error Event is fired.

9 Focus Handler

void on Focus(Focus Event event);

Called when Focus Event is fired.

10 Form Panel.Submit Complete Handler

void on Submit Complete(Form Panel.Submit Complete Event event);

Fired when a form has been submitted successfully.

11 FormPanel.SubmitHandler

void on Submit(Form Panel.Submit Event event);

Fired when the form is submitted.

12 Key Down Handler

void on Key Down(Key Down Event event);

Called when KeyDownEvent is fired.

13 KeyPressHandler

void on KeyPress(KeyPressEvent event);

Called when KeyPressEvent is fired.

14 KeyUpHandler

void on KeyUp(KeyUpEvent event);

Called when KeyUpEvent is fired.

15 LoadHandler

void on Load(LoadEvent event);

Called when LoadEvent is fired.

16 MouseDownHandler

void on MouseDown(MouseDownEvent event);

Called when MouseDown is fired.

17 MouseMoveHandler

void on MouseMove(MouseMoveEvent event);

Called when MouseMoveEvent is fired.

18 MouseOutHandler

void on MouseOut(MouseOutEvent event);

Called when MouseOutEvent is fired.

19 MouseOverHandler

void on MouseOver(MouseOverEvent event);

Called when MouseOverEvent is fired.

20 MouseUpHandler

void on MouseUp(MouseUpEvent event);

Called when MouseUpEvent is fired.

21 MouseWheelHandler

void on MouseWheel(MouseWheelEvent event);

Called when MouseWheelEvent is fired.

22 ResizeHandler

void on Resize(ResizeEvent event);

Fired when the widget is resized.

23 ScrollHandler

void on Scroll(ScrollEvent event);

Called when ScrollEvent is fired.

24 SelectionHandler

void on Selection(SelectionEvent event);

Called when SelectionEvent is fired.

25 ValueChangeHandler

void on ValueChange(ValueChangeEvent event);

Called when ValueChangeEvent is fired.

26 Window.ClosingHandler

void on WindowClosing(Window.ClosingEvent event);

Fired just before the browser window closes or navigates to a different site.

27 Window.ScrollHandler

void on WindowScroll(Window.ScrollEvent event);

Fired when the browser window is scrolled.

事件方法

如前所述,每个处理程序都有一个带有单个参数的方法,该方法包含事件对象,例如void onClick(ClickEvent event)void onKeyDown(KeyDownEvent event) 。诸如ClickEventKeyDownEvent之类的事件对象具有一些常见的方法,在下面列出-

Sr.No. Method & Description
1

protected void dispatch(ClickHandler handler) This method Should only be called by HandlerManager

2

DomEvent.Type getAssociatedType() This method returns the type used to register Foo event.

3

static DomEvent.TypegetType() This method gets the event type associated with Foo events.

4

public java.lang.Object getSource() This method returns the source that last fired this event.

5

protected final boolean isLive() This method returns whether the event is live.

6

protected void kill() This method kills the event

本示例将带您完成简单的步骤,以显示GWT中Click事件和KeyDown事件处理的用法。请按照以下步骤更新我们在GWT-创建应用程序一章中创建的GWT应用程序

Step Description
1 Create a project with a name HelloWorld under a package com.tutorialspoint as explained in the GWT – Create Application chapter.
2 Modify HelloWorld.gwt.xml, HelloWorld.css, HelloWorld.html and HelloWorld.java as explained below. Keep rest of the files unchanged.
3 Compile and run the application to verify the result of the implemented logic.

以下是修改后的模块描述符src / com.tutorialspoint / HelloWorld.gwt.xml的内容



   
   

   
   

   
   

   
   
   


以下是修改后的样式表文件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的内容

Hello World
      
      
   

   
      

Event Handling Demonstration

让我们有以下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-创建应用程序一章中那样,以开发模式编译和运行应用程序。如果您的应用程序一切正常,这将产生以下结果-

GWT事件处理