📄 131-135.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=131-135//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="128-131.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="135-138.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading23"></A><FONT COLOR="#000077">The Evt Class</FONT></H4>
<P>The <I>Evt</I> class is a public static class that contains a series of methods that address the software engineering problem cited in the preceding section. The basic assumption is that several different event <I>combinations</I> can cause a single result, the example being a keypress event and a menu item event. The solution involves the creation of an overloaded <I>match</I> method:</P>
<!-- CODE //-->
<PRE>
class Evt {
...
public static boolean matchKey(Event e, int key) {
return ((e.id == Event.KEY_PRESS) && (e.key==key));
}
public static boolean match(Event e, int key, String str) {
return (matchKey(e,key) || e.arg.equals(str));
}
public static boolean match(Event e, int key, Object target) {
return (matchKey(e,key) || e.target.equals(target));
}
...
</PRE>
<!-- END CODE //-->
<P>The <I>Evt</I> class offers the immediate benefit of giving a two-way match between keyboard and <I>menuItem</I> events. In the <I>AudioFrame</I> class, the <I>keyDown</I> method is eliminated and replaced with this:</P>
<!-- CODE SNIP //-->
<PRE>
public boolean handleEvent(Event e) {
...
if (Evt.match(e,’2’,ifft_mi)) {
ifft();
return true;
}
...
</PRE>
<!-- END CODE SNIP //-->
<P>This single convention eliminates 60 lines of code in the <I>AudioFrame keyDown</I> event handler. To add another level of automation to the event processing, we assume that the keyboard shortcut will be encoded into the string representation of the <I>menuItem</I>, as it appears in the main menu bar. Figure 3.6 shows a sample main menu bar from the <I>AudioFrame</I> in the DiffCAD program.</P>
<P><A NAME="Fig6"></A><A HREF="javascript:displayWindow('images/03-06.jpg',136,353 )"><IMG SRC="images/03-06t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/03-06.jpg',136,353)"><FONT COLOR="#000077"><B>Figure 3.6</B></FONT></A> Sample AudioFrame main menu bar drop-down menu.</P>
<P>One feature of the <I>AudioFrame</I> keyboard shortcuts is that they conform to this convention: If a keyboard shortcut exists, it is encoded by a [ followed by a single character. Tognazzini reports that $50 million of research led to two conclusions: Users say keyboard shortcuts are faster, and the stopwatch shows that mouse choices are faster. Tognazzini produced a guideline that states that visual interface construction should not have its resources sapped by the keyboard interface [Tognazzini]. As a result of this common sense suggestion, we have combined the following check into a single method called <I>match</I>:</P>
<!-- CODE SNIP //-->
<PRE>
public static boolean match(Event e, Object target) {
int c = getKeyboardShortCut(e, target);
return match(e, c, target);
}
</PRE>
<!-- END CODE SNIP //-->
<P>In the following code, <I>getKeyboardShortCut</I> makes three assumptions if a keyboard shortcut is embedded in the target: The target will be an instance of a <I>MenuItem</I>, the first character of the label will be a [ and the second character of the label will be the keyboard shortcut character.</P>
<!-- CODE //-->
<PRE>
public static int getKeyboardShortCut(Event e, Object target) {
if (target instanceof MenuItem) {
MenuItem mi = (MenuItem) target;
String str = mi.getLabel();
int index = str.indexOf(‘[‘);
if (index == 0) { return str.charAt(1);}
}
return -1;
}
</PRE>
<!-- END CODE //-->
<P>This approach leads to a more consistent interface (keyboard shortcuts are always encoded visually and consistently), an easier-to-use API (only one cal, instead of two), and better software engineering (centralized interface changes with fewer lines of code). Keyboard and menu item events are now handled as follows:
</P>
<!-- CODE SNIP //-->
<PRE>
...
public boolean handleEvent(Event e) {
...
if (Evt.match(e,ifft_mi)) {
ifft();
return true;
}
...
</PRE>
<!-- END CODE SNIP //-->
<P>It takes no more effort to encode and process a keyboard shortcut than to process and set up a visual interface, fulfilling Tognazzini’s basic guideline.:
</P>
<!-- CODE SNIP //-->
<PRE>
MenuItem ifft_mi = addItem(“[2] IFFT”);
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading24"></A><FONT COLOR="#000077">The Mouse</FONT></H4>
<P>Any pick device (such as a trackball, penlight, touch pad, or tablet) can generate mouse events. The Java AWT supports only a single pick device, and, no matter what the pick device is, it is said to generate mouse events. To handle the mouse events, the AWT <I>Component</I> class provides a suite of callback methods.</P>
<P>When the mouse button is depressed:</P>
<!-- CODE SNIP //-->
<PRE>
public boolean mouseDown(Event evt, int x, int y)
</PRE>
<!-- END CODE SNIP //-->
<P>When the mouse button is depressed and the mouse is moved:
</P>
<!-- CODE SNIP //-->
<PRE>
public boolean mouseDrag(Event evt, int x, int y)
</PRE>
<!-- END CODE SNIP //-->
<P>When the mouse button is released:
</P>
<!-- CODE SNIP //-->
<PRE>
public boolean mouseUp(Event evt, int x, int y)
</PRE>
<!-- END CODE SNIP //-->
<P>When the mouse is moved:
</P>
<!-- CODE SNIP //-->
<PRE>
public boolean mouseMove(Event evt, int x, int y)
</PRE>
<!-- END CODE SNIP //-->
<P>When the mouse is over a component:
</P>
<!-- CODE SNIP //-->
<PRE>
public boolean mouseEnter(Event evt, int x, int y)
</PRE>
<!-- END CODE SNIP //-->
<P>When the mouse leaves the component:
</P>
<!-- CODE SNIP //-->
<PRE>
public boolean mouseExit(Event evt, int x, int y)
</PRE>
<!-- END CODE SNIP //-->
<P>To implement a mouse event, you must either override the preceding events or use a <I>switch</I> statement to process the <I>Event.id</I>. The choice between <I>switch</I> statement and method is a question of preference, policy, and good software engineering practice. The <I>switch</I> statement is more flexible and permits more “combination” events (such as a shift-click for extending a selection).</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR>In the following code fragment taken from <I>GUI.java</I>, a <I>handleEvent</I> method is implemented with a <I>switch</I> statement<HR></FONT>
</BLOCKQUOTE>
<!-- CODE SNIP //-->
<PRE>
1. public boolean handleEvent(Event e) {
2. switch (e.id) {
</PRE>
<!-- END CODE SNIP //-->
<P>On line 3, the <I>Event.MOUSE_UP</I> indicates that the mouse button was released. When this occurs, the <I>processMouseUp</I> method is invoked, causing an object to change its appearance.</P>
<!-- CODE SNIP //-->
<PRE>
3. case Event.MOUSE_UP: {
4. String objectName = mouse_choice.getSelectedItem();
5. processMouseUp( objectName, e.x, e.y);
6. repaint();
7. }
</PRE>
<!-- END CODE SNIP //-->
<P>On line 8, the <I>Event.MOUSE_DOWN</I> triggers the recording of the location of the mouse cursor. The anchor point is used to provide relative mouse motion.</P>
<!-- CODE SNIP //-->
<PRE>
8. case Event.MOUSE_DOWN:
9. anchor = new point( e.x, e.y);
10. } // switch
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="128-131.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="135-138.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 + -