📄 chapter13.html
字号:
TextField t1 = <font color=#0000ff>new</font> TextField(<font color=#004488>"t1"</font>, 10);
Label labl1 = <font color=#0000ff>new</font> Label(<font color=#004488>"TextField t1"</font>);
Label labl2 = <font color=#0000ff>new</font> Label(<font color=#004488>" "</font>);
Label labl3 = <font color=#0000ff>new</font> Label(<font color=#004488>" "</font>,
Label.RIGHT);
Button b1 = <font color=#0000ff>new</font> Button(<font color=#004488>"Test 1"</font>);
Button b2 = <font color=#0000ff>new</font> Button(<font color=#004488>"Test 2"</font>);
<font color=#0000ff>public</font> <font color=#0000ff>void</font> init() {
add(labl1); add(t1);
add(b1); add(labl2);
add(b2); add(labl3);
}
<font color=#0000ff>public</font> <font color=#0000ff>boolean</font> action (Event evt, Object arg) {
<font color=#0000ff>if</font>(evt.target.equals(b1))
labl2.setText(<font color=#004488>"Text set into Label"</font>);
<font color=#0000ff>else</font> <font color=#0000ff>if</font>(evt.target.equals(b2)) {
<font color=#0000ff>if</font>(labl3.getText().trim().length() == 0)
labl3.setText(<font color=#004488>"labl3"</font>);
<font color=#0000ff>if</font>(labl3.getAlignment() == Label.LEFT)
labl3.setAlignment(Label.CENTER);
<font color=#0000ff>else</font> <font color=#0000ff>if</font>(labl3.getAlignment()==Label.CENTER)
labl3.setAlignment(Label.RIGHT);
<font color=#0000ff>else</font> <font color=#0000ff>if</font>(labl3.getAlignment() == Label.RIGHT)
labl3.setAlignment(Label.LEFT);
}
<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>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The first use of the label is the
most typical: labeling a <B>TextField</B> or <B>TextArea</B>. In the second part
of the example, a bunch of empty spaces are reserved and when you press the
“Test 1” button <B>setText( )</B> is used to insert text into
the field. Because a number of blank spaces do not equal the same number of
characters (in a
<A NAME="Index1626"></A><A NAME="Index1627"></A><A NAME="Index1628"></A>proportionally-spaced
font) you’ll see that the text gets truncated when inserted into the
label.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The third part of the example
reserves empty space, then the first time you press the “Test 2”
button it sees that there are no characters in the label (since
<A NAME="Index1629"></A><B>trim( )</B> removes all of the blank spaces at
each end of a <B>String</B>) and inserts a short label, which is initially
left-aligned. The rest of the times you press the button it changes the
alignment so you can see the effect.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You might think that you could
create an empty label and then later put text in it with <B>setText( )</B>.
However, you cannot put text into an empty label – presumably because it
has zero width – so creating a label with no text seems to be a useless
thing to do. In the example above, the “blank” label is filled with
empty spaces so it has enough width to hold text that’s placed inside
later.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Similarly, <B>setAlignment( )
</B>has no effect on a label that you’d typically create with text in the
constructor. The label width is the width of the text, so changing the alignment
doesn’t do anything. However, if you start with a long label and then
change it to a shorter one you can see the effect of the
alignment.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">These behaviors occur because of
the default <A NAME="Index1630"></A><A NAME="Index1631"></A><I>layout
manager</I> that’s used for applets, which causes things to be squished
together to their smallest size. Layout managers will be covered later in this
chapter, when you’ll see that other layouts don’t have the same
effect.</FONT><A NAME="_Toc375545452"></A><A NAME="_Toc408018688"></A><BR></P></DIV>
<A NAME="Heading403"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Check boxes</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A check box provides a way to make
a single on-off choice; it consists of a tiny box and a label. The box typically
holds a little ‘x’ (or some other indication that it is set) or is
empty depending on whether that item was selected.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You’ll normally create a
<A NAME="Index1632"></A><A NAME="Index1633"></A><B>Checkbox</B> using a
constructor that takes the label as an argument. You can get and set the state,
and also get and set the label if you want to read or change it after the
<B>Checkbox</B> has been created. Note that the capitalization of
<B>Checkbox</B> is inconsistent with the other controls, which could catch you
by surprise since you might expect it to be
“CheckBox.”</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Whenever a <B>Checkbox</B> is set
or cleared an event occurs, which you can capture the same way you do a button.
The following example uses a <B>TextArea</B> to enumerate all the check boxes
that have been checked:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: CheckBox1.java</font>
<font color=#009900>// Using check boxes</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> CheckBox1 <font color=#0000ff>extends</font> Applet {
TextArea t = <font color=#0000ff>new</font> TextArea(6, 20);
Checkbox cb1 = <font color=#0000ff>new</font> Checkbox(<font color=#004488>"Check Box 1"</font>);
Checkbox cb2 = <font color=#0000ff>new</font> Checkbox(<font color=#004488>"Check Box 2"</font>);
Checkbox cb3 = <font color=#0000ff>new</font> Checkbox(<font color=#004488>"Check Box 3"</font>);
<font color=#0000ff>public</font> <font color=#0000ff>void</font> init() {
add(t); add(cb1); add(cb2); add(cb3);
}
<font color=#0000ff>public</font> <font color=#0000ff>boolean</font> action (Event evt, Object arg) {
<font color=#0000ff>if</font>(evt.target.equals(cb1))
trace(<font color=#004488>"1"</font>, cb1.getState());
<font color=#0000ff>else</font> <font color=#0000ff>if</font>(evt.target.equals(cb2))
trace(<font color=#004488>"2"</font>, cb2.getState());
<font color=#0000ff>else</font> <font color=#0000ff>if</font>(evt.target.equals(cb3))
trace(<font color=#004488>"3"</font>, cb3.getState());
<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=#0000ff>void</font> trace(String b, <font color=#0000ff>boolean</font> state) {
<font color=#0000ff>if</font>(state)
t.appendText(<font color=#004488>"Box "</font> + b + <font color=#004488>" Set\n"</font>);
<font color=#0000ff>else</font>
t.appendText(<font color=#004488>"Box "</font> + b + <font color=#004488>" Cleared\n"</font>);
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>trace( )</B> method
sends the name of the selected <B>Checkbox</B> and its current state to the
<B>TextArea</B> using
<A NAME="Index1634"></A><A NAME="Index1635"></A><B>appendText( )</B> so
you’ll see a cumulative list of the checkboxes that were selected and what
their state
is.</FONT><A NAME="_Toc375545453"></A><A NAME="_Toc408018689"></A><BR></P></DIV>
<A NAME="Heading404"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Radio buttons</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The concept of a
<A NAME="Index1636"></A><A NAME="Index1637"></A><A NAME="Index1638"></A>radio
button in GUI programming comes from pre-electronic car radios with mechanical
buttons: when you push one in, any other button that was pressed pops out. Thus
it allows you to force a single choice among many.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The AWT does not have a separate
class to represent the radio button; instead it reuses the
<A NAME="Index1639"></A><A NAME="Index1640"></A><B>Checkbox</B>. However, to put
the <B>Checkbox</B> in a radio button group (and to change its shape so
it’s visually different from an ordinary <B>Checkbox</B>) you must use a
special constructor that takes a
<A NAME="Index1641"></A><A NAME="Index1642"></A><B>CheckboxGroup</B> object as
an argument. (You can also call
<A NAME="Index1643"></A><A NAME="Index1644"></A><B>setCheckboxGroup( )</B>
after the <B>Checkbox</B> has been created.)</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A <B>CheckboxGroup </B>has no
constructor argument; its sole reason for existence is to collect some
<B>Checkbox</B>es into a group of radio buttons. One of the <B>Checkbox</B>
objects must have its state set to <B>true</B> before you try to display the
group of radio buttons; otherwise you’ll get an exception at run time. If
you try to set more than one radio button to <B>true</B> then only the final one
set will be <B>true</B>.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here’s a simple example of
the use of radio buttons. Note that you capture radio button events like all
others:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: RadioButton1.java</font>
<font color=#009900>// Using radio buttons</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> RadioButton1 <font color=#0000ff>extends</font> Applet {
TextField t =
<font color=#0000ff>new</font> TextField(<font color=#004488>"Radio button 2"</font>, 30);
CheckboxGroup g = <font color=#0000ff>new</font> CheckboxGroup();
Checkbox
cb1 = <font color=#0000ff>new</font> Checkbox(<font color=#004488>"one"</font>, g, <font color=#0000ff>false</font>),
cb2 = <font color=#0000ff>new</font> Checkbox(<font color=#004488>"two"</font>, g, <font color=#0000ff>true</font>),
cb3 = <font color=#0000ff>new</font> Checkbox(<font color=#004488>"three"</font>, g, <font color=#0000ff>false</font>);
<font color=#0000ff>public</font> <font color=#0000ff>void</font> init() {
t.setEditable(<font color=#0000ff>false</font>);
add(t);
add(cb1); add(cb2); add(cb3);
}
<font color=#0000ff>public</font> <font color=#0000ff>boolean</font> action (Event evt, Object arg) {
<font color=#0000ff>if</font>(evt.target.equals(cb1))
t.setText(<font color=#004488>"Radio button 1"</font>);
<font color=#0000ff>else</font> <font color=#0000ff>if</font>(evt.target.equals(cb2))
t.setText(<font color=#004488>"Radio button 2"</font>);
<font color=#0000ff>else</font> <font color=#0000ff>if</font>(evt.target.equals(cb3))
t.setText(<font color=#004488>"Radio button 3"</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>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To display the state, an text field
is used. This field is set to non-editable because it’s used only to
display data, not to collect it. This is shown as an alternative to using a
<B>Label</B>. Notice the text in the field is initialized to “Radio button
2” since that’s the initial selected radio button.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can have any number of
<B>CheckboxGroup</B>s on a
form.</FONT><A NAME="_Toc375545454"></A><A NAME="_Toc408018690"></A><BR></P></DIV>
<A NAME="Heading405"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Drop-down lists</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Like a group of radio buttons, a
<A NAME="Index1645"></A><A NAME="Index1646"></A><A NAME="Index1647"></A>drop-down
list is a way to force the user to select only one element from a group of
possibilities. However, it’s a much more compact way to accomplish this,
and it’s easier to change the elements of the list without surprising the
user. (You can change radio buttons dynamically, but that tends to be visibly
jarring).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Java’s
<A NAME="Index1648"></A><A NAME="Index1649"></A><B>Choice</B> box is not like
the combo box in Windows, which lets you select from a list <I>or</I> type in
your own selection. With a <B>Choice</B> box you choose one and only one element
from the list. In the following example, the <B>Choice</B> box starts with a
certain number of entries and then new entries are added to the box when a
button is pressed. This allows you to see some interesting behaviors in
<B>Choice</B> boxes:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -