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

📄 tij0145.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html><body>

<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0144.html">Prev</a> | <a href="tij0146.html">Next</a>
</td>
</tr></table>
<hr>

<H2 ALIGN=LEFT>
Alternatives
to action
</H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">As
noted previously, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
isn&#8217;t the only method that&#8217;s automatically called by <A NAME="Index1715"></A><A NAME="Index1716"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
once it sorts everything out for you. There are three other sets of methods
that are called, and if you want to capture certain types of events (keyboard,
mouse, and focus events) all you have to do is override the provided method.
These methods are defined in the base class <A NAME="Index1717"></A><A NAME="Index1718"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Component</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
so they&#8217;re available in virtually all the controls that you might place
on a form. However, you should be aware that this approach is deprecated in
Java 1.1<A NAME="Index1719"></A>,
so although you might see legacy code using this technique you should use the
Java 1.1 approaches (described later in this chapter) instead.
</FONT><P></DIV>
<DIV ALIGN=LEFT><TABLE BORDER>
<COLGROUP>
      <COL width="184">
      <COL width="157">
</COLGROUP>
<TR VALIGN="TOP">
<TH WIDTH=184 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Component
method
</FONT><P></DIV>
</TH>
<TH WIDTH=157 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
it&#8217;s called
</FONT><P></DIV>
</TH>
</TR>
<A NAME="Index1720"></A><A NAME="Index1721"></A><TR VALIGN="TOP">
<TD WIDTH=184 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action
(Event evt, Object what)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=157 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
the &#8220;typical&#8221; event occurs for this component (for example, when a
button is pushed or a drop-down list item is selected)
</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1722"></A><A NAME="Index1723"></A><TR VALIGN="TOP">
<TD WIDTH=184 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>keyDown
(Event evt, int key)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=157 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
key is pressed when this component has the focus. The second argument is the
key that was pressed and is redundantly copied from 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>evt.key</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1724"></A><A NAME="Index1725"></A><TR VALIGN="TOP">
<TD WIDTH=184 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>keyUp(Event
evt, int key)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=157 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
key is released when this component has the focus.
</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1726"></A><A NAME="Index1727"></A><TR VALIGN="TOP">
<TD WIDTH=184 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>lostFocus(Event
evt, Object what)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=157 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
focus has moved away from the target. Normally, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>what</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is redundantly copied from 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>evt.arg</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1728"></A><A NAME="Index1729"></A><TR VALIGN="TOP">
<TD WIDTH=184 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>gotFocus(Event
evt, Object what)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=157 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
focus has moved into the target.
</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1730"></A><A NAME="Index1731"></A><TR VALIGN="TOP">
<TD WIDTH=184 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mouseDown(Event
evt, 
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
 int x, int y)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=157 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
mouse down has occurred over the component, at the coordinates 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>x</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>y</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1732"></A><A NAME="Index1733"></A><TR VALIGN="TOP">
<TD WIDTH=184 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mouseUp(Event
evt, int x, int y)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=157 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
mouse up has occurred over the component.
</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1734"></A><A NAME="Index1735"></A><TR VALIGN="TOP">
<TD WIDTH=184 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mouseMove(Event
evt, int x, int y)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=157 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
mouse has moved while it&#8217;s over the component.
</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1736"></A><A NAME="Index1737"></A><TR VALIGN="TOP">
<TD WIDTH=184 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mouseDrag(Event
evt, int x, int y)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=157 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
mouse is being dragged after a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mouseDown</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
occurred over the component. All drag events are reported to the component in
which the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mouseDown</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
occurred until there is a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mouseUp</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1738"></A><A NAME="Index1739"></A><TR VALIGN="TOP">
<TD WIDTH=184 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mouseEnter(Event
evt, int x, int y)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=157 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
mouse wasn&#8217;t over the component before, but now it is.
</FONT><P></DIV>
</TD>
</TR>
<A NAME="Index1740"></A><A NAME="Index1741"></A><TR VALIGN="TOP">
<TD WIDTH=184 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mouseExit(Event
evt, int x, int y)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=157 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
mouse used to be over the component, but now it isn&#8217;t.
</FONT><P></DIV>
</TD>
</TR>
</TABLE></DIV>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can see that each method receives an 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Event</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object along with some information that you&#8217;ll typically need when
you&#8217;re handling that particular situation &#8211; with a mouse event, for
example, it&#8217;s likely that you&#8217;ll want to know the coordinates where
the mouse event occurred. It&#8217;s interesting to note that when 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Component</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;s
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
calls any of these methods (the typical case), the extra arguments are always
redundant as they are contained within 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. In fact, if you look at the source code for 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Component.handleEvent(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
you can see that it explicitly plucks the additional arguments out of 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. (This might be considered inefficient coding in some languages, but
remember that Java&#8217;s focus is on safety, not necessarily speed.)
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
prove to yourself that these events are in fact being called and as an
interesting experiment, it&#8217;s worth creating an applet that overrides each
of the methods above (except for 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action(&#160;),
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">which
is overridden in many other places in this chapter) and displays data about
each of the events as they happen.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
example also shows you how to make your own button object because that&#8217;s
what is used as the target of all the events of interest. You might first
(naturally) assume that to make a new button, you&#8217;d inherit from <A NAME="Index1742"></A><A NAME="Index1743"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Button</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
But this doesn&#8217;t work. Instead, you inherit from <A NAME="Index1744"></A><A NAME="Index1745"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Canvas</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(a much more generic component) and paint your button on that canvas by
overriding the <A NAME="Index1746"></A><A NAME="Index1747"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>paint(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method. As you&#8217;ll see, it&#8217;s really too bad that overriding 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Button</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
doesn&#8217;t work, since there&#8217;s a bit of code involved to paint the
button. (If you don&#8217;t believe me, try exchanging 

⌨️ 快捷键说明

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