⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 131-135.html

📁 dshfghfhhgsfgfghfhfghgfhfghfgh fg hfg hh ghghf hgf hghg gh fg hg hfg hfh f hg hgfh gkjh kjkh g yj f
💻 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   &#123;
...
  public static boolean matchKey(Event e, int key) &#123;
       return ((e.id == Event.KEY_PRESS) &#38;&#38; (e.key==key));
  &#125;
  public static  boolean match(Event e, int key, String str) &#123;
       return (matchKey(e,key) || e.arg.equals(str));
  &#125;
  public static  boolean match(Event e, int key, Object target) &#123;
       return (matchKey(e,key) || e.target.equals(target));
  &#125;
...
</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) &#123;
...
          if (Evt.match(e,&#146;2&#146;,ifft_mi)) &#123;
               ifft();
               return true;
          &#125;
...
</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>&nbsp;&nbsp;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) &#123;
      int c = getKeyboardShortCut(e, target);
  return match(e, c, target);
 &#125;
</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) &#123;
      if (target instanceof MenuItem) &#123;
           MenuItem mi = (MenuItem) target;
           String str = mi.getLabel();
           int index = str.indexOf(&#145;[&#145;);
           if (index == 0) &#123; return str.charAt(1);&#125;
           &#125;
      return -1;
 &#125;
</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) &#123;
...
          if (Evt.match(e,ifft_mi)) &#123;
               ifft();
               return true;
          &#125;
...
</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&#146;s basic guideline.:
</P>
<!-- CODE SNIP //-->
<PRE>
    MenuItem ifft_mi          = addItem(&#147;[2] IFFT&#148;);
</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 &#147;combination&#148; 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) &#123;
2.     switch (e.id) &#123;
</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: &#123;
4.          String objectName = mouse_choice.getSelectedItem();
5.          processMouseUp( objectName, e.x, e.y);
6.          repaint();
7.          &#125;
</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.     &#125; // 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 &copy; <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 + -