📄 tij0143.html
字号:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Inside
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
it’s possible that the button has the focus and someone is typing to it.
Whether it makes sense or not, those are events that you can detect and act
upon in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent( )</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">To
modify the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
example so that it will react to single mouse clicks, the button detection will
be left in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
but the code to handle the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
will be moved into
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
as follows:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: List2.java</font>
<font color="#009900">// Using lists with handleEvent()</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> List2 <font color="#0000ff">extends</font> Applet {
String[] flavors = { "Chocolate", "Strawberry",
"Vanilla Fudge Swirl", "Mint Chip",
"Mocha Almond Fudge", "Rum Raisin",
"Praline Cream", "Mud Pie" };
<font color="#009900">// Show 6 items, allow multiple selection:</font>
List lst = <font color="#0000ff">new</font> List(6, <font color="#0000ff">true</font>);
TextArea t = <font color="#0000ff">new</font> TextArea(flavors.length, 30);
Button b = <font color="#0000ff">new</font> Button("test");
<font color="#0000ff">int</font> count = 0;
<font color="#0000ff">public</font> <font color="#0000ff">void</font> init() {
t.setEditable(<font color="#0000ff">false</font>);
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < 4; i++)
lst.addItem(flavors[count++]);
add(t);
add(lst);
add(b);
}
<font color="#0000ff">public</font> <font color="#0000ff">boolean</font> handleEvent(Event evt) {
<font color="#0000ff">if</font>(evt.id == Event.LIST_SELECT ||
evt.id == Event.LIST_DESELECT) {
<font color="#0000ff">if</font>(evt.target.equals(lst)) {
t.setText("");
String[] items = lst.getSelectedItems();
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < items.length; i++)
t.appendText(items[i] + "\n");
}
<font color="#0000ff">else</font>
<font color="#0000ff">return</font> <font color="#0000ff">super</font>.handleEvent(evt);
}
<font color="#0000ff">else</font>
<font color="#0000ff">return</font> <font color="#0000ff">super</font>.handleEvent(evt);
<font color="#0000ff">return</font> <font color="#0000ff">true</font>;
}
<font color="#0000ff">public</font> <font color="#0000ff">boolean</font> action(Event evt, Object arg) {
<font color="#0000ff">if</font>(evt.target.equals(b)) {
<font color="#0000ff">if</font>(count < flavors.length)
lst.addItem(flavors[count++], 0);
}
<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">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
example is the same as before except for the addition of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Inside, a check is made to see whether a list selection or deselection has
occurred. Now remember,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent( )
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
being overridden for the applet, so this occurrence could be anywhere on the
form and it could be happening to another list. Thus, you must also check to
see what the target is. (Although in this case there’s only one list on
the applet so we could have made the assumption that all list events must be
about that list. This is bad practice since it’s going to be a problem as
soon as another list is added.) If the list matches the one we’re
interested in, the same code as before will do the trick.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Note
that the form for
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent( )
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
similar to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">if
you deal with a particular event you
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>return</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>true</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
but if you’re not interested in any of the other events via
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
you must
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>return
<A NAME="Index1665"></A><A NAME="Index1666"></A>super.handleEvent(evt)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This is vital because if you don’t do this, none of the other
event-handling code will get called. For example, try commenting out the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>return
super.handleEvent(evt)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in the code above. You’ll discover that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
never gets called, certainly not what you want. For both
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
it’s important to follow the format above and always return the
base-class version of the method when you do not handle the event yourself (in
which case 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">).
(Fortunately, these kinds of bug-prone details are relegated to Java 1.0<A NAME="Index1667"></A>.
The new design in Java 1.1<A NAME="Index1668"></A>
that you will see later in the chapter eliminates these kinds of issues.)
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
Windows, a list box automatically allows <A NAME="Index1669"></A><A NAME="Index1670"></A><A NAME="Index1671"></A>multiple
selections if you hold down the shift key. This is nice because it allows the
user to choose a single or multiple selection rather than fixing it during
programming. You might think you’ll be clever and implement this yourself
by checking to see if the shift key is held down when a mouse click was made by
testing for
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>evt.shiftDown( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Alas, the design of the AWT stymies you – you’d have to be able to
know which item was clicked on if the shift key
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>wasn’t
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">pressed
so you could deselect all the rest and select only that one. However, you
cannot figure that out in Java 1.0.<A NAME="Index1672"></A>
(Java 1.1<A NAME="Index1673"></A>
sends all mouse, keyboard, and focus events to a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
so you’ll be able to accomplish this.)
</FONT><a name="_Toc375545457"></a><a name="_Toc408018693"></a><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0142.html">Prev</a> | <a href="tij0144.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -