您的位置:首页 > 编程语言 > Java开发

《Java 编程技巧1001条》 第403条: 了解事件类

2017-12-23 11:10 597 查看



《Java 编程技巧1001条》第11章 事件驱动 第403条 了解事件类403 Understanding the Event Class
403 了解事件类(Event class)
As you will learn, Java generates an Event object when something happens (an event occurs) for which the applet must respond. An Event object is an instance of the Event class, and stores information about the event. For example, when you click the mouse, Java generates an event object that contains the mouse screen coordinates. Likewise, when you press a keyboard key, Java generates a keyboard event that contains the keys ASCII code. When an event occurs, Java calls a series of methods based on the event type. Table 403 shows some of the information stored in the Event object.
你将会知道,当应用块必须响应的某种事情发生时,Java就会生成一个Event类. Event对象是Event类的一个实例,它保存着有关事件的信息. 例如,当你在鼠标器上击键时,Java将产生一个事件,用来保存鼠标的屏幕坐标. 同样,当你在键盘上按下一个键时,Java将产生一个键盘事件,用来保存按键的ASCII码. 当一事件发生时,Java将根据事件的类型而调用一系列的方法. 表403指出了存于Event对象中的几种信息.
Type     Name          Description
---------------------------------------------------------------------
int           id             Contains the event type identifier
Object  target         Object where event occurred
int     clickCount     Multiple mouse click flag
int         key            Key code for keyboard events
int     modifiers      Modifier keys state when event occurred
long    when           Time when event occurred
int         x,y            Coordinates of mouse events
---------------------------------------------------------------------
   Table 403 Event class variables.

类型    名称           说明
---------------------------------------------------------------------
int          id             包含事件类型标识符
Object  target         事件发生的对象
int     clickCount     鼠标重击标志
int         key            键盘事件的击键码
int      modifiers      事件发生时变动键的状态
long    when           事件发生的时间
int          x,y            鼠标事件的光标位置
---------------------------------------------------------------------
   Table 403 Event class variables.
   表403 事件类变量

Not all events have valid data for all the variables in the Event object. For example, it does not make sense tocheck the clickCount variable for a keydown eventthe clickCount variable corresponds to mouse double-click 
operations. The following tips will show you how to use the Event class data.
在Event对象中,并不是所有变量都产生有用的数据. 例如,检测击键事件的clickCount变量是没有意义的. clickCount变量仅用于区别鼠标的单击和双击. 以下TIP将向你显示怎样使用Event类数据. 

404 Understanding Convenience Methods
40 了解Convenience方法 
In most event-driven programming languages, the program sits in a loop 
waiting for events to occur. Within this event loop, you place code that 
decides which part of your program should receive the event. Based on your 
code, the program dispatches events by calling appropriate methods.
Within a Java applet, Java provides this event-processing loop behind the 
scenes for you. Also, Java defines a set of convenience methods to help 
your program distinguish between events. A convenience method is how Java 
tells your program that a particular type of Event has occurred. For 
example, if a user clicks the mouse, Java will call the mouseDown 
convenience method.
在大多数事件驱动程序设计语言中,程序处在一循环中等待事件的发生. 在这事件
循环中, 你加入了一段代码来确定你的程序的哪一部分应接收事件. 根据你的代码,
程序调用不同的方法来处理事件. 在Java应用程序中,Java在后台为你提供了这种事件
处理循环. 同样,Java定义了许多能帮助你的程序区别不同事件的Convenience方法. 其
中一个Convenience方法是Java怎样来告诉你一种特别类型的事件已发生. 例如, 如果
用户按下了mouse键,Java将调用mouseDown方法.
Java declares convenience methods in the Component class. You will learn 
the details of the Component class in later tips. For now, you just need to 
know that the Applet class is a subclass of the Component class which 
defines many of component objects every program needs (such as the 
convenience methods). Consequently, you can use the convenience methods in 
any class you extend from the Applet or Component classes. 

Java在Component类中声明Convenience方法. 你将在今后的TIP中学习Component类的
细节. 现在,你只需知道Applet类是Component类的一个子类,它定义了许多每一个程
序都要用的Component对象(例如Convenience方法). 因此, 你可以在由Applet类或
Component类扩充而得到的任何类中使用Convenience方法.

Within your applet, you can have more than one object that extends from the 
Component class. Each of these objects can have its own convenience 
methods. Java will call the correct methods based on the type of event that 
occurred.
在你的应用程序中,你可以有多个由Componet类扩充而来的对象. 这些对象每一个都可
以有自己的Convenience方法. Java将根据发生的事件的类型来调用正确的方法.

405 Using the mouseDown Method
405 使用mouseDown方法 
As your applets become interactive, they will need to respond to mouse and 
keyboard events. One of Java's convenience methods is the mouseDown method, 
which your programs can use to detect when a user has held down the mouse 
button. The following program, mouseDownApplet.java, displays the 
mouse-screen coordinates every time the user clicks the mouse (causing Java 
to call the mouseDown method): 
当你的程序是交互式程序时,它们必需响应鼠标事件和键盘事件.Java的Convenience方
法之一是mouseDown方法,你可以用它来检测何时用户按下了Mouse键. 以下这一程序,
mouseDownApplet.java,在每当用户点击鼠标(导致Java调用mouseDown方法)显示mouse
的屏幕坐标.
kaj test this
import java.awt.*;
import java.applet.*;
 
public class mouseDownApplet extends Applet {

   public boolean mouseDown(Event evt, int x, int y)
     {
       System.out.println("Mouse Down (" + x + "," + y + ")");
       return(true);
     }
 }

The mouseDown methods first argument is the Event object. The second and 
third arguments are the (x and y) screen coordinates of the mouse pointer 
at the time the user clicked the mouse. The screen coordinates specify the 
distance in pixels from the upper-left corner of the display to the mouse 
pointers location. In the previous example, the applet windows upper-left 
corner is location (0, 0).
mouseDown方法的第一个自变量是Event变量.第二和第三个自变量(x,y)是用户点击
mouse时的mouse光标的屏幕坐标. 屏幕坐标指示mouse光标与显示器左上角的距离,
用pixel为单位来计算. 在前一例子中,applet窗口左上角的位置是(0,0).

Note that the mouseDown method returns true. When you create an event 
handler within your applets, your method should return the value true to 
tell Java that you have handled the event. If Java receives the true 
value, Java will not call any other convenience methods to handle the 
event. If your event handler returns false, Java will call another event 
handler to process the event. 
注意,这里mouseDown方法返回的是true.当你在你的applet中创建一个事件句柄(event
handler)时,你的方法必须返回true值以通知Java你已取得了事件信息. 如果Java接受
了true值,Java就不再调用其它的convenience方法来处理事件. 如果你事件句柄返回
了false,则Java将会调用其它的事件处理程序来处理事件.

406 Using the mouseUp method
405 使用mouseUp方法 
In the previous tip, you learned about the mouseDown event which Java calls 
when the user holds down the mouse button. Conversely, Java provides the 
mouseUp event when the user releases the mouse button. If you are not 
familiar with writing graphical user interfaces, you may wonder why there 
are two mouse-click events. A common use of the mouse-down event is to 
highlight a control, such as a button, that the user has selected. If the 
user releases the mouse button over the control, the program knows the user 
has accepted the button. However, if the user moves the mouse off the 
control before the mouse is released, the program knows the user did not 
select the button.
在以上TIP中,你已知道了mouseDown方法是用户按住mouse按钮时Java所调用的. 相反
地, Java也提供一个mouseUp方法是用户释放mouse按钮时产生的. 如果你不熟悉用图
形用户接口编程, 你可能对为什么要两个mouse击键事键感到奇怪. mouseDown方法的
普通用途是强调显示(highlight)用户选择的一个控制,如button(按钮). 如果用户在
控制上释放了mouse按钮,程序知道用户是接受了button. 如果用户在mouse释放之前
把mouse移开了控制,程序将知道用户没有选择此button.

The following program, mouseUpApplet.java, displays a message to the screen 
every time the user holds down the mouse button and also when the user 
releases the button. The program also displays the mouse-screen coordinates 
for each event:
以下的mouseUpApplet.java程序在每当用户按着mouse button和释放mouse button时
在屏幕上显示一个消息. 程序同时也为每一事件显示mouse光标的屏幕坐标:
import java.awt.*;
import java.applet.*;
 
public class mouseUpApplet extends Applet {
   public boolean mouseDown(Event evt, int x, int y)
     {
       System.out.println("Mouse Down (" + x + "," + y + ")");
       return(true);
     }

   public boolean mouseUp(Event evt, int x, int y)
     {
       System.out.println("Mouse Up (" + x + "," + y + ")");
       return(true);
     }
 }
Take time now to run this applet. Next, move and click the mouse both from 
within and outside of the applet window to learn how Java responds to such 
mouse operations.
现在来运行这一应用程序.然后移动mouse,并在应用块窗口的内部和外部点击mouse键,
以了解Java是怎样响应这些mouse操作的.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: