📄 128-131.html
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1558515682"><META name=vstitle content="Java Digital Signal Processing"><META name=vsauthor content="Douglas A. Lyon"><META name=vsimprint content="M&T Books"><META name=vspublisher content="IDG Books Worldwide, Inc."><META name=vspubdate content="11/01/97"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Java Digital Signal Processing:The Graphical User Interface</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<!--ISBN=1558515682//-->
<!--TITLE=Java Digital Signal Processing//-->
<!--AUTHOR=Douglas A. Lyon//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=3//-->
<!--PAGES=128-131//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="125-128.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="131-135.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading19"></A><FONT COLOR="#000077">Class Usage</FONT></H4>
<P>Suppose the following constants are predefined:
</P>
<!-- CODE SNIP //-->
<PRE>
Object target;
long when; // a creation time stamp
int id; // the event type
int x, y; // event location
int key;
int modifiers;
Object arg;
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>target</I> is the instance of the <I>Component</I> class that posted the event. The <I>when</I> member holds a creation time stamp measured in milliseconds. The <I>id</I> permits decoding of the event type with the use of a <I>switch</I> statement. The location of the event is contained in (x,y). The state of the modifier keys is encoded into the <I>modifiers</I> parameter. <I>Arg</I> is the instance of the class that is to be associated with the event. The <I>Event</I> class constructor is overloaded so that we can write the following:</P>
<!-- CODE SNIP //-->
<PRE>
Event evt = new Event(target, id, arg);
Event evt = new Event(target, when, id, x, y, key, modifiers);
Event evt = new Event(target, when, id, x, y, key, modifiers, arg);
</PRE>
<!-- END CODE SNIP //-->
<P>Typically, it is the user’s job to write code to decode the events. Unfortunately, the services provided by the AWT lead to an ad hoc approach for event handling that results in poor software engineering. The AWT assumes that keyboard events and menu choice events should be handled in different parts of the source code. Often, keyboard shortcuts are used to speed menu selections. Thus, menu selections are processed in a different part of the source code from the keyboard event, leading to duplication of code. Additional development effort is needed to add keyboard shortcuts. A remedy for this design flaw in the AWT is discussed in the following section.
</P>
<P>Several public variables are associated with the <I>Event</I> class. Some of the public variables are key masks designed to help with decoding. An example of this is shown in the <I>Component</I> section:</P>
<!-- CODE SNIP //-->
<PRE>
KEY_EVENT, SHIFT_MASK, CTRL_MASK,META_MASK, ALT_MASK, HOME, END, PGUP,
PGDN,KEY_PRESS, KEY_RELEASE, KEY_ACTION, KEY_ACTION_RELEASE
</PRE>
<!-- END CODE SNIP //-->
<P>The arrow keys are decoded with the following:
</P>
<!-- CODE SNIP //-->
<PRE>
UP, DOWN, LEFT, RIGHT
</PRE>
<!-- END CODE SNIP //-->
<P>The function keys are decoded with these variables:
</P>
<!-- CODE SNIP //-->
<PRE>
F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12
</PRE>
<!-- END CODE SNIP //-->
<P>The window events are decoded with
</P>
<!-- CODE SNIP //-->
<PRE>
WINDOW_EVENT, WINDOW_DESTROY, WINDOW_EXPOSE, WINDOW_ICONIFY,
WINDOW_DEICONIFY, WINDOW_MOVED, WINDOW_EVENT
</PRE>
<!-- END CODE SNIP //-->
<P>The mouse events are decoded with
</P>
<!-- CODE SNIP //-->
<PRE>
MOUSE_EVENT, MOUSE_DOWN, MOUSE_UP, MOUSE_MOVE, MOUSE_ENTER, MOUSE_EXIT,
MOUSE_DRAG
</PRE>
<!-- END CODE SNIP //-->
<P>The scrolling events are decoded with
</P>
<!-- CODE SNIP //-->
<PRE>
SCROLL_EVENT, SCROLL_LINE_UP, SCROLL_LINE_DOWN, SCROLL_PAGE_UP,
SCROLL_PAGE_DOWN, SCROLL_ABSOLUTE
</PRE>
<!-- END CODE SNIP //-->
<P>The list events are decoded with
</P>
<!-- CODE SNIP //-->
<PRE>
LIST_EVENT, LIST_SELECT, LIST_DESELECT
</PRE>
<!-- END CODE SNIP //-->
<P>A series of events are classified as miscellaneous events:
</P>
<!-- CODE SNIP //-->
<PRE>
MISC_EVENT, ACTION_EVENT,LOAD_FILE, SAVE_FILE, GOT_FOCUS, LOST_FOCUS
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading20"></A><FONT COLOR="#000077">Event Handling</FONT></H4>
<P>In part because of its modularization, the AWT model for handling events can lead to poor software engineering. The AWT designers sought to map mouse events, keyboard events, and menu selection events using a preprocessing facility built into the <I>Component</I> class. This policy is based on the assumption that the relationship between event production and code invocation is isomorphic. This assumption is fallacious because multiple events can often be used to trigger a single task. Menu choices often have keyboard shortcuts, and both a menu event and a keypress event can trigger the same processing.</P>
<H4 ALIGN="LEFT"><A NAME="Heading21"></A><FONT COLOR="#000077">The Keyboard</FONT></H4>
<P>To handle keyboard events in the AWT, a class that extends the <I>Component</I> class will typically have its <I>keyDown</I> method invoked. In the following code fragment, the <I>AudioFrame</I> class extends the <I>Frame</I> class (which is two generations removed from the <I>Component</I> superclass):</P>
<!-- CODE //-->
<PRE>
...
public class AudioFrame extends Frame {
...
public boolean keyDown(Event e, int key) {
switch (key) {
case ‘2’:
ifft();
return true;
}
System.out.println(“Unknown key function:”+ key);
return true;
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading22"></A><FONT COLOR="#000077">The Target</FONT></H4>
<P>To handle the passing of an instance, the <I>Event</I> class supports a <I>target</I> member that is of <I>Object</I> type. To process this event, a <I>handleEvent</I> method is used:</P>
<!-- CODE //-->
<PRE>
...
public class AudioFrame extends Frame {
...
Menu m = new Menu(“Audio Menu”);
...
MenuItem ifft_mi = addItem(“[2] IFFT”);
public MenuItem addItem(String itemName) {
MenuItem mi = new MenuItem(itemName);
m.add(mi);
return(mi);
}
public void init_menu() {
// my menu items
MenuBar menuBar = new MenuBar();
// Initialize the menu bar
setMenuBar(menuBar);
menuBar.add(m);
...
public boolean handleEvent(Event e) {
...
if (e.target == ifft_mi) {
ifft();
return true;
}
...
</PRE>
<!-- END CODE //-->
<P>Notice that the keyboard event calls the same code that the <I>handleEvent</I> calls, because the <I>MenuItem</I> instance (which appears in the main menu bar) has a keyboard shortcut. This is unsound software engineering, because parallel maintenance must be performed in both the keyboard and menu event processing code. A sounder approach appears in the following section.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="125-128.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="131-135.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright © <a href="/reference/idgbooks00001.html">IDG Books Worldwide, Inc.</a></font></div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --></BODY></HTML><!-- END FOOTER -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -