📄 chapter13.html
字号:
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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="Index1595"></A><A NAME="Index1596"></A><A NAME="Index1597"></A><A NAME="Index1598"></A>Web
browser window on its <I>status line</I> by calling the <B>Applet</B> method
<A NAME="Index1599"></A><A NAME="Index1600"></A><B>getAppletContext( )</B>
to get access to the browser and then
<A NAME="Index1601"></A><A NAME="Index1602"></A><B>showStatus( )</B> to put
a string on the status
line.</FONT><A NAME="fnB53" HREF="#fn53">[53]</A><FONT FACE="Georgia"> You
can print out a complete description of an event the same way, with
<B>getAppletContext().showStatus(evt + "" ). </B>(The empty <B>String</B> forces
the compiler to convert <B>evt</B> to a <B>String.</B>) Both of these reports
are really useful only for testing and debugging since the browser might
overwrite your message.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Strange as it might seem, you can
also <A NAME="Index1603"></A><A NAME="Index1604"></A>match an event to the
<I>text</I> that’s on a button through the second argument in
<B>event( )</B>. Using this technique, the example above
becomes:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><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(<font color=#004488>"Button 1"</font>),
b2 = <font color=#0000ff>new</font> Button(<font color=#004488>"Button 2"</font>);
<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(<font color=#004488>"Button 1"</font>))
getAppletContext().showStatus(<font color=#004488>"Button 1"</font>);
<font color=#0000ff>else</font> <font color=#0000ff>if</font>(arg.equals(<font color=#004488>"Button 2"</font>))
getAppletContext().showStatus(<font color=#004488>"Button 2"</font>);
<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>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It’s difficult to know
exactly what the <B>equals( )</B> 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><BR></P></DIV>
<A NAME="Heading400"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Text fields</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A
<A NAME="Index1605"></A><A NAME="Index1606"></A><B>TextField</B> is a one line
area that allows the user to enter and edit text. <B>TextField </B>is inherited
from <A NAME="Index1607"></A><A NAME="Index1608"></A><B>TextComponent</B>,<B>
</B>which lets you select text, get the selected text as a <B>String</B>, get or
set the text, and set whether the <B>TextField</B> is editable, along with other
associated methods that you can find in your online reference. The following
example demonstrates some of the functionality of a <B>TextField</B>; you can
see that the method names are fairly obvious:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: TextField1.java</font>
<font color=#009900>// Using the text field control</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> TextField1 <font color=#0000ff>extends</font> Applet {
Button
b1 = <font color=#0000ff>new</font> Button(<font color=#004488>"Get Text"</font>),
b2 = <font color=#0000ff>new</font> Button(<font color=#004488>"Set Text"</font>);
TextField
t = <font color=#0000ff>new</font> TextField(<font color=#004488>"Starting text"</font>, 30);
String s = <font color=#0000ff>new</font> String();
<font color=#0000ff>public</font> <font color=#0000ff>void</font> init() {
add(b1);
add(b2);
add(t);
}
<font color=#0000ff>public</font> <font color=#0000ff>boolean</font> action (Event evt, Object arg) {
<font color=#0000ff>if</font>(evt.target.equals(b1)) {
getAppletContext().showStatus(t.getText());
s = t.getSelectedText();
<font color=#0000ff>if</font>(s.length() == 0) s = t.getText();
t.setEditable(<font color=#0000ff>true</font>);
}
<font color=#0000ff>else</font> <font color=#0000ff>if</font>(evt.target.equals(b2)) {
t.setText(<font color=#004488>"Inserted by Button 2: "</font> + s);
t.setEditable(<font color=#0000ff>false</font>);
}
<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>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There are several ways to construct
a <B>TextField</B>; the one shown here provides an initial string and sets the
size of the field in characters.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Pressing button 1 either gets the
text you’ve selected with the mouse or it gets all the text in the field
and places the result in <B>String s</B>. It also allows the field to be edited.
Pressing button 2 puts a message and <B>s</B> into the text field and prevents
the field from being edited (although you can still select the text). The
editability of the text is controlled by passing
<A NAME="Index1609"></A><A NAME="Index1610"></A><B>setEditable( )</B> a
<B>true</B> or
<B>false</B>.</FONT><A NAME="_Toc375545450"></A><A NAME="_Toc408018686"></A><BR></P></DIV>
<A NAME="Heading401"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Text areas</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A
<A NAME="Index1611"></A><A NAME="Index1612"></A><B>TextArea</B> is like a
<B>TextField</B> except that it can have multiple lines and has significantly
more functionality. In addition to what you can do with a <B>TextField</B>, you
can append text and insert or replace text at a given location. It seems like
this functionality could be useful for <B>TextField</B> as well, so it’s a
little confusing to try to detect how the distinction is made. You might think
that if you want <B>TextArea</B> functionality everywhere you can simply use a
one line <B>TextArea</B> in places where you would otherwise use a
<B>TextField</B>. In Java 1.0<A NAME="Index1613"></A>, you also got scroll bars
with a <B>TextArea </B>even when they weren’t appropriate; that is, you
got both vertical and horizontal scroll bars for a one line <B>TextArea</B>. In
Java 1.1<A NAME="Index1614"></A> this was remedied with an extra constructor
that allows you to select which scroll bars (if any) are present. The following
example shows only the Java 1.0<A NAME="Index1615"></A> behavior, in which the
scrollbars are always on. Later in the chapter you’ll see an example that
demonstrates Java 1.1 <B>TextArea</B>s.</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: TextArea1.java</font>
<font color=#009900>// Using the text area control</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> TextArea1 <font color=#0000ff>extends</font> Applet {
Button b1 = <font color=#0000ff>new</font> Button(<font color=#004488>"Text Area 1"</font>);
Button b2 = <font color=#0000ff>new</font> Button(<font color=#004488>"Text Area 2"</font>);
Button b3 = <font color=#0000ff>new</font> Button(<font color=#004488>"Replace Text"</font>);
Button b4 = <font color=#0000ff>new</font> Button(<font color=#004488>"Insert Text"</font>);
TextArea t1 = <font color=#0000ff>new</font> TextArea(<font color=#004488>"t1"</font>, 1, 30);
TextArea t2 = <font color=#0000ff>new</font> TextArea(<font color=#004488>"t2"</font>, 4, 30);
<font color=#0000ff>public</font> <font color=#0000ff>void</font> init() {
add(b1);
add(t1);
add(b2);
add(t2);
add(b3);
add(b4);
}
<font color=#0000ff>public</font> <font color=#0000ff>boolean</font> action (Event evt, Object arg) {
<font color=#0000ff>if</font>(evt.target.equals(b1))
getAppletContext().showStatus(t1.getText());
<font color=#0000ff>else</font> <font color=#0000ff>if</font>(evt.target.equals(b2)) {
t2.setText(<font color=#004488>"Inserted by Button 2"</font>);
t2.appendText(<font color=#004488>": "</font> + t1.getText());
getAppletContext().showStatus(t2.getText());
}
<font color=#0000ff>else</font> <font color=#0000ff>if</font>(evt.target.equals(b3)) {
String s = <font color=#004488>" Replacement "</font>;
t2.replaceText(s, 3, 3 + s.length());
}
<font color=#0000ff>else</font> <font color=#0000ff>if</font>(evt.target.equals(b4))
t2.insertText(<font color=#004488>" Inserted "</font>, 10);
<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>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There are several different
<B>TextArea</B> constructors, but the one shown here gives a starting string and
the number of rows and columns. The different buttons show getting, appending,
replacing, and inserting
text.</FONT><A NAME="_Toc375545451"></A><A NAME="_Toc408018687"></A><BR></P></DIV>
<A NAME="Heading402"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Labels</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A
<A NAME="Index1616"></A><A NAME="Index1617"></A><B>Label</B> does exactly what
it sounds like it should: places a label on the form. This is particularly
important for text fields and text areas that don’t have labels of their
own, and can also be useful if you simply want to place textual information on a
form. You can, as shown in the first example in this chapter, use
<B>drawString( )</B> inside <B>paint( )</B> to place text in an exact
location. When you use a <B>Label</B> it allows you to (approximately) associate
the text with some other component via the layout manager (which will be
discussed later in this chapter).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">With the constructor you can create
a blank label, a label with initial text in it (which is what you’ll
typically do), and a label with an alignment of <B>CENTER</B>, <B>LEFT</B>, or
<B>RIGHT</B> (<B>static final int</B>s defined in class <B>Label</B>). You can
also change the label and its alignment with
<A NAME="Index1618"></A><A NAME="Index1619"></A><B>setText( )</B> and
<A NAME="Index1620"></A><A NAME="Index1621"></A><B>setAlignment( )</B>, and
if you’ve forgotten what you’ve set these to you can read the values
with <A NAME="Index1622"></A><A NAME="Index1623"></A><B>getText( )</B> and
<A NAME="Index1624"></A><A NAME="Index1625"></A><B>getAlignment( )</B>.
This example shows what you can do with labels:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: Label1.java</font>
<font color=#009900>// Using labels</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> Label1 <font color=#0000ff>extends</font> Applet {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -