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

📄 tij0186.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 2 页
字号:
  <font color="#0000ff">public</font> String getNext() {
    String s = <font color="#0000ff">null</font>;
    <font color="#0000ff">try</font> {
      <font color="#0000ff">if</font>(st.nextToken() !=
            StreamTokenizer.TT_EOF) {
        <font color="#0000ff">switch</font>(st.ttype) {
          <font color="#0000ff">case</font> StreamTokenizer.TT_EOL:
            s = <font color="#0000ff">null</font>;
            <font color="#0000ff">break</font>;
          <font color="#0000ff">case</font> StreamTokenizer.TT_NUMBER:
            s = Double.toString(st.nval);
            <font color="#0000ff">break</font>;
          <font color="#0000ff">case</font> StreamTokenizer.TT_WORD:
            s = <font color="#0000ff">new</font> String(st.sval);
            <font color="#0000ff">break</font>;
          <font color="#0000ff">default</font>: <font color="#009900">// single character in ttype</font>
            s = String.valueOf((<font color="#0000ff">char</font>)st.ttype);
        }
      }
    } <font color="#0000ff">catch</font>(IOException e) {
      System.out.println(e);
    }
    <font color="#0000ff">return</font> s;
  }
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> String strip(String qualified) {
    StripQualifiers sq = 
      <font color="#0000ff">new</font> StripQualifiers(qualified);
    String s = "", si;
    <font color="#0000ff">while</font>((si = sq.getNext()) != <font color="#0000ff">null</font>) {
      <font color="#0000ff">int</font> lastDot = si.lastIndexOf('.');
      <font color="#0000ff">if</font>(lastDot != -1)
        si = si.substring(lastDot + 1);
      s += si;
    }
    <font color="#0000ff">return</font> s;
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Some
things you&#8217;ve seen before. As with many of the GUI programs in this book,
this is created to perform both as an application and as an applet. Also, the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StripQualifiers</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class is exactly the same as it was in Chapter 11.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
GUI contains a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>TextField</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>name
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">in
which you can enter the fully-qualified class name you want to look up, and
another one, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>searchFor</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
in which you can enter the optional text to search for within the list of
methods. The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Checkbox</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
allows you to say whether you want to use the fully-qualified names in the
output or if you want the qualification stripped off. Finally, the results are
displayed in a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>TextArea</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You&#8217;ll
notice that there are no buttons or other components by which to indicate that
you want the search to start. That&#8217;s because both of the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>TextField</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
and the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Checkbox</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
are monitored by their listener objects. Whenever you make a change, the list
is immediately updated. If you change the text within the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>name
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">field,
the new text is captured in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>class
NameL
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
If the text isn&#8217;t empty, it is used inside <A NAME="Index3065"></A><A NAME="Index3066"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class.forName(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to try to look up the class. As you&#8217;re typing, of course, the name will
be incomplete and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class.forName(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
will fail, which means that it throws an exception. This is trapped and the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>TextArea</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is set to &#8220;No match&#8221;. But as soon as you type in a correct name
(capitalization counts), 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class.forName(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is successful and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getMethods(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getConstructors(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
will return arrays of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Method</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Constructor</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects, respectively. Each of the objects in these arrays is turned into a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
via 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>toString(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(this produces the complete method or constructor signature) and both lists are
combined into 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>n</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
a single 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
array. The array 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>n</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is a member of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>class
DisplayMethods
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and is used in updating the display whenever 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>reDisplay(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is called.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
you change the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Checkbox</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>searchFor</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
components, their listeners simply call 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>reDisplay(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>reDisplay(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
creates a temporary array of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
called 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>rs</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(for &#8220;result set&#8221;). The result set is either copied directly from 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>n</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
if there is no 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>find</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
word, or conditionally copied from the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>n</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that contain the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>find</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
word. Finally, the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>strip</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Checkbox</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is interrogated to see if the user wants the names to be stripped (the default
is &#8220;yes&#8221;). If so, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StripQualifiers.strip(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
does the job; if not, the list is simply displayed.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>init(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you might think that there&#8217;s a lot of busy work involved in setting up
the layout. In fact, it is possible to lay out the components with less work,
but the advantage of using 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BorderLayout</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
this way is that it allows the user to resize the window and make &#8211; in
particular &#8211; the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>TextArea</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
larger, which means you can resize to allow you to see longer names without
scrolling.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
might find that you&#8217;ll keep this tool running while you&#8217;re
programming, since it provides one of the best &#8220;first lines of
attack&#8221; when you&#8217;re trying to figure out what method to call.
</FONT><a name="_Toc408018814"></a><P></DIV>

<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0185.html">Prev</a> | <a href="tij0187.html">Next</a>
</div>
</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -