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

📄 164-167.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=164-167//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="160-164.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="167-168.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading51"></A><FONT COLOR="#000077">The Choice Class</FONT></H3>
<P>The <I>Choice</I> component class provides a pop-up menu of string choices. The currently selected string becomes the title of the string menu. In many ways, a <I>Choice</I> component is like a main menu bar selection that continues to display its last selection. Pop-up menu examples can be seen in Figure 3.9, and Figure 3.10.</P>
<H4 ALIGN="LEFT"><A NAME="Heading52"></A><FONT COLOR="#000077">Class Summary</FONT></H4>
<!-- CODE //-->
<PRE>
public class Choice extends Component &#123;
    public Choice()
    public synchronized void addNotify()
    public int countItems()
    public String getItem(int index)
    public synchronized void addItem(String item)
    public String getSelectedItem()
    public int getSelectedIndex()
    public synchronized void select(int pos)
    public void select(String str)
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading53"></A><FONT COLOR="#000077">Class Usage</FONT></H4>
<P>The <I>Choice</I> component has only a single constructor. Suppose the following variables are predefined:</P>
<!-- CODE SNIP //-->
<PRE>
Choice choice = new Choice();
int i;
String str;
</PRE>
<!-- END CODE SNIP //-->
<P>To create the peer, use this code:
</P>
<!-- CODE SNIP //-->
<PRE>
choice.addNotify();
</PRE>
<!-- END CODE SNIP //-->
<P>To find the number of strings in <I>choice</I>:</P>
<!-- CODE SNIP //-->
<PRE>
i = choice.countItems();
</PRE>
<!-- END CODE SNIP //-->
<P>To get the string at location <I>i</I>:</P>
<!-- CODE SNIP //-->
<PRE>
str = choice.getItem(i);
</PRE>
<!-- END CODE SNIP //-->
<P>To add a string:
</P>
<!-- CODE SNIP //-->
<PRE>
choice.addItem(str);
</PRE>
<!-- END CODE SNIP //-->
<P>To get the selected string:
</P>
<!-- CODE SNIP //-->
<PRE>
str = choice.getSelectedItem();
</PRE>
<!-- END CODE SNIP //-->
<P>To get the location of the string in the list:
</P>
<!-- CODE SNIP //-->
<PRE>
i = choice.getSelectedIndex();
</PRE>
<!-- END CODE SNIP //-->
<P>To select a string, by position or name, thereby making it visible:
</P>
<!-- CODE SNIP //-->
<PRE>
choice.select(i);
choice.select(str);
&#125;
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading54"></A><FONT COLOR="#000077">Adding Choices to a Frame</FONT></H4>
<P>In this example, we show how to add a <I>Choice</I> menu to a <I>Frame</I>.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR>The following code is excerpted from the DiffCAD program in <I>GUI.java</I>.<HR></FONT>
</BLOCKQUOTE>
<P>The basic idea is that a graph object can be selected by choosing it from the <I>Choice</I> instance.</P>
<!-- CODE //-->
<PRE>
public class GUI  extends Applet implements constants  &#123;
    Choice mouseChoice;
    String objectPropertiesStr = new String(&#147;Object Properties&#148;);
    String cameraStr = new String(&#147;camera&#148;);
    String laserStr = new String(&#147;laser&#148;);
    String laserAngleStr = new String(&#147;laser angle&#148;);
    String gratingStr = new String(&#147;grating&#148;);
    String graphPvsXStr = new String(&#147;graph p vs x&#148;);
    String graphDLvsXStr = new String(&#147;graph DL vs x&#148;);
    String allStr = new String(&#147;all&#148;);

        String mouseChoiceItems[] = &#123;
         objectPropertiesStr,
         cameraStr, laserStr, laserAngleStr, gratingStr,
         graphPvsXStr, graphDLvsXStr, allStr
    &#125;;
</PRE>
<!-- END CODE //-->
<P>To assist in the creation of <I>Choice</I> menus, we use the graphics utilities class Guitils:</P>
<!-- CODE //-->
<PRE>
public class Guitils &#123;
     static public Choice addChoiceMenu(String items[],
      Container cont) &#123;
        Choice choice  = new Choice();
        for (int i = 0; i &lt; items.length; i&#43;&#43;) &#123;
             choice.addItem(items[i]);
        &#125;
        cont.add(choice);
        return  choice;
     &#125;   ...
</PRE>
<!-- END CODE //-->
<P><I>GUI.java</I> uses the <I>Guitils.addChoiceMenu</I> method in <I>create_gui</I>:</P>
<!-- CODE SNIP //-->
<PRE>
  public void create_gui() &#123;
        setBackground(Color.white);
     Guitils.addCheckBoxes(checkboxes, this);
     mouseChoice = Guitils.addChoiceMenu(mouseChoiceItems, this);
</PRE>
<!-- END CODE SNIP //-->
<P>Because the <I>Frame</I> class is a subclass of the <I>Container</I> class, the usage of <I>this</I> passes the <I>Frame</I>, with automatic type casting, into the <I>Guitils.addCheckBoxes</I> method. The <I>Guitils.addCheckBoxes</I> method does not keep a reference to the <I>Choice</I> instance, <I>choice</I>, because the <I>Container</I> instance will store it.</P>
<P>To handle the <I>choice</I>, we have adopted the policy of waiting for the mouse to be released. Then, we can process the <I>choice</I>:</P>
<!-- CODE //-->
<PRE>
1. public boolean handleEvent(Event e) &#123;
2.      switch (e.id) &#123;
3.           case Event.MOUSE_UP: &#123;
4.                     String objectName = mouseChoice.getSelectedItem();
5.                     processMouseUp( objectName, e.x, e.y);
6.                     repaint();
7.            &#125;
8.            case Event.MOUSE_DOWN:
9.                 anchor = new point( e.x, e.y);
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="160-164.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="167-168.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 + -