📄 tij0143.html
字号:
<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="tij0142.html">Prev</a> | <a href="tij0144.html">Next</a>
</td>
</tr></table>
<hr>
<H2 ALIGN=LEFT>
List
boxes
<P><A NAME="Index1651"></A><A NAME="Index1652"></A></H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">List
boxes are significantly different from
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Choice</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
boxes, and not just in appearance. While a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Choice</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
box drops down when you activate it, a <A NAME="Index1653"></A><A NAME="Index1654"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
occupies some fixed number of lines on a screen all the time and doesn’t
change. In addition, a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
allows multiple selection: if you click on more than one item the original item
stays highlighted and you can select as many as you want. If you want to see
the items in a list, you simply call <A NAME="Index1655"></A><A NAME="Index1656"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getSelectedItems( ),
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">which
produces an 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">
of the items that have been selected. To remove an item from a group you have
to click it again.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
problem with a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is that the default action is double clicking, not single clicking. A single
click adds or removes elements from the selected group and a double click calls
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
One way around this is to re-educate your user, which is the assumption made in
the following program:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: List1.java</font>
<font color="#009900">// Using lists with action()</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> List1 <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> action (Event evt, Object arg) {
<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">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">When
you press the button it adds items to the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>top</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
of the list (because of the second argument 0 to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>addItem( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
Adding elements 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">is
more reasonable than the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Choice</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
box because users expect to scroll a list box (for one thing, it has a built-in
scroll bar) but they don’t expect to have to figure out how to get a
drop-down list to scroll, as in the previous example.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">However,
the only way for
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to be called is through a double-click. If you need to monitor other activities
that the user is doing on your
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(in particular, single clicks) you must take an alternative approach.
</FONT><a name="_Toc375545456"></a><a name="_Toc408018692"></a><P></DIV>
<A NAME="Heading408"></A><H3 ALIGN=LEFT>
handleEvent( )</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">So
far we’ve been using
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
but there’s another method that gets first crack at everything: <A NAME="Index1657"></A><A NAME="Index1658"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Any time an event happens, it happens “over” or “to” a
particular object. The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method for that object is automatically called and an <A NAME="Index1659"></A><A NAME="Index1660"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Event</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object is created and passed to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The default
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(which is defined in <A NAME="Index1661"></A><A NAME="Index1662"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Component</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
the base class for virtually all the “controls” in the AWT) will
call either <A NAME="Index1663"></A><A NAME="Index1664"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action( ),</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
as we’ve been using, or other similar methods to indicate mouse activity,
keyboard activity, or to indicate that the focus has moved. We’ll look at
those later in this chapter.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">What
if these other methods –
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in particular – don’t satisfy your needs? In the case of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
for example, what if you want to catch single mouse clicks but
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
responds to only double clicks? The solution is to override
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for your applet, which after all is derived from
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Applet</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and can therefore override any non-
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
methods. When you override
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>handleEvent( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for the applet you’re getting all the applet events before they are
routed, so you cannot just assume “This has to do with my button so I can
assume it’s been pressed,” since that’s true only for
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -