📄 tij0136.html
字号:
getAppletContext().showStatus("Button 1");
<font color="#0000ff">else</font> <font color="#0000ff">if</font>(evt.target.equals(b2))
getAppletContext().showStatus("Button 2");
<font color="#009900">// Let the base class handle it:</font>
<font color="#0000ff">else</font>
<font color="#0000ff">return</font> <font color="#0000ff">super</font>.action(evt, arg);
<font color="#0000ff">return</font> <font color="#0000ff">true</font>; <font color="#009900">// We've handled it here</font>
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
see what the <A NAME="Index1591"></A><A NAME="Index1592"></A>target
is, ask the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Event</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object what its
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>target</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
member is and then use the <A NAME="Index1593"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>equals( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method to see if it matches the target object handle you’re interested
in. When you’ve written handlers for all the objects you’re
interested in you must call <A NAME="Index1594"></A><A NAME="Index1595"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>super.action(evt,
arg)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>else</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement at the end, as shown above. Remember from Chapter 7 (polymorphism)
that your overridden method is called instead of the base class version.
However, the base-class version contains code to handle all of the cases that
you’re not interested in, and it won’t get called unless you call
it explicitly. The return value indicates whether you’ve handled it or
not, so if you do match an event you should return
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>true</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
otherwise return whatever the base-class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>event( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
returns.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">For
this example, the simplest action is to print what button is pressed. Some
systems allow you to pop up a little window with a message in it, but applets
discourage this. However, you can put a message at the bottom of the <A NAME="Index1596"></A><A NAME="Index1597"></A><A NAME="Index1598"></A><A NAME="Index1599"></A>Web
browser window on its
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>status
line
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
by calling the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Applet</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method <A NAME="Index1600"></A><A NAME="Index1601"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getAppletContext( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to get access to the browser and then <A NAME="Index1602"></A><A NAME="Index1603"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>showStatus( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to put a string on the status line.
</FONT><A NAME="fnB56" HREF="#fn56">[56]</A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
You can print out a complete description of an event the same way, with
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getAppletContext().showStatus(evt
+ "" ).
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">(The
empty
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
forces the compiler to convert
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>evt</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String.</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
Both of these reports are really useful only for testing and debugging since
the browser might overwrite your message.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Strange
as it might seem, you can also <A NAME="Index1604"></A><A NAME="Index1605"></A>match
an event to the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>text</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that’s on a button through the second argument in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>event( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Using this technique, the example above becomes:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: Button3.java</font>
<font color="#009900">// Matching events on button text</font>
<font color="#0000ff">import</font> java.awt.*;
<font color="#0000ff">import</font> java.applet.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> Button3 <font color="#0000ff">extends</font> Applet {
Button
b1 = <font color="#0000ff">new</font> Button("Button 1"),
b2 = <font color="#0000ff">new</font> Button("Button 2");
<font color="#0000ff">public</font> <font color="#0000ff">void</font> init() {
add(b1);
add(b2);
}
<font color="#0000ff">public</font> <font color="#0000ff">boolean</font> action (Event evt, Object arg) {
<font color="#0000ff">if</font>(arg.equals("Button 1"))
getAppletContext().showStatus("Button 1");
<font color="#0000ff">else</font> <font color="#0000ff">if</font>(arg.equals("Button 2"))
getAppletContext().showStatus("Button 2");
<font color="#009900">// Let the base class handle it:</font>
<font color="#0000ff">else</font>
<font color="#0000ff">return</font> <font color="#0000ff">super</font>.action(evt, arg);
<font color="#0000ff">return</font> <font color="#0000ff">true</font>; <font color="#009900">// We've handled it here</font>
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">It’s
difficult to know exactly what the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>equals( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method is doing here. The biggest problem with this approach is that most new
Java programmers who start with this technique spend at least one frustrating
session discovering that they’ve gotten the capitalization or spelling
wrong when comparing to the text on a button. (I had this experience.) Also, if
you change the text of the button, the code will no longer work (but you
won’t get any compile-time or run-time error messages). You should avoid
this approach if possible.
</FONT><a name="_Toc375545449"></a><a name="_Toc408018685"></a><P></DIV>
<HR><DIV ALIGN=LEFT><A NAME="fn56" HREF="#fnB56">[56]</A><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black"><B>ShowStatus( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">
is also a method of Applet, so you can call it directly, without calling
</FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black"><B>getAppletContext( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">.</FONT><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0135.html">Prev</a> | <a href="tij0137.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -