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

📄 ch19.htm

📁 Java_by_Example,初级经典例子哦,珍藏版本
💻 HTM
📖 第 1 页 / 共 3 页
字号:
class.<BR>    Declare three checkbox objects.<BR>    Override the <TT>init()</TT> method.<BR>        Create the three checkboxes.<BR>        Add the checkboxes to the applet's display.<BR>    Override the <TT>action()</TT> method.<BR>        If a checkbox caused the event, call the <TT>ChangeLabel()</TT>method.<BR>        Force Java to redraw the applet's display.<BR>        Tell Java that the event was handled okay.<BR>    Declare the <TT>ChangeLabel()</TT> method.<BR>        Cast the <TT>Event</TT> object to a <TT>Checkbox</TT>object.<BR>        Get the checkbox's label.<BR>        Change the label of the selected checkbox<BR>        Or else change all the labels back to their normal form.</BLOCKQUOTE><P>When you run the CheckboxApplet2 applet, you see a display somethinglike Figure 19.5. You can click on any of the checkboxes as normal,and their check state will change accordingly. However, when youclick on a checkbox, its label will also change, as shown in Figure19.6, proving that the applet is responding to the event generatedby the checkbox. If you click on a checkbox that still has itsoriginal label, that label changes. If you click on a label thathas already been changed, all the checkbox labels revert to theirstarting text.<P><A HREF="f19-5.gif"><B> Figure 19.5 : </B><I>This is CheckboxApplet2 running under Appletviewer.</I></A><P><P><A HREF="f19-6.gif"><B> Figure 19.6 : </B><I>Clicking the checkboxes changes their labels.</I></A><P><H2><A NAME="TextFields"><FONT SIZE=5 COLOR=#Ff0000>TextFields</FONT></A></H2><P>You've already had a lot of experience with textfield controls.You've used these handy controls to implement text input for manyof your previous applets. As you already know, a textfield object,which is an object of the <TT>TextField</TT> class, is much likea Windows edit control, providing a small box into which the usercan type text. To create a textfield control, you call the <TT>TextField</TT>class's constructor, like this:<BLOCKQUOTE><PRE>TextField textField = new TextField(str, size);</PRE></BLOCKQUOTE><P>The constructor's two arguments are the default text that shouldbe displayed in the textfield control and the size in charactersof the control. After you create the control, add it to the appletby calling the <TT>add()</TT> method, like this:<BLOCKQUOTE><PRE>add(textField);</PRE></BLOCKQUOTE><H3><A NAME="ITextFieldIMethods"><I>TextField</I> Methods</A></H3><P>The <TT>TextField</TT> class features a number of public methodsthat you can use to manipulate textfield objects. By using thesemethods, you can set a textfield object's characteristics andobtain information about the object. Table 19.2 lists the mostcommonly used methods and their descriptions.<BR><P><CENTER><B>Table 19.2&nbsp;&nbsp;Methods of the </B><I>TextField</I><B>Class.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=289><I><B>Method</B></I></TD><TD WIDTH=301><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=289><TT>boolean echoCharIsSet()</TT></TD><TD WIDTH=301>Returns <TT>true</TT> if the object's echo character is set. When set, echo characters appear in place of any character the user types.</TD></TR><TR VALIGN=TOP><TD WIDTH=289><TT>int getColumns()</TT></TD><TD WIDTH=301>Returns the size of the textfield object.</TD></TR><TR VALIGN=TOP><TD WIDTH=289><TT>char getEchoChar()</TT></TD><TD WIDTH=301>Returns the object's echo character, if set.</TD></TR><TR VALIGN=TOP><TD WIDTH=289><TT>String getText()</TT></TD><TD WIDTH=301>Gets the text from the textfield object.</TD></TR><TR VALIGN=TOP><TD WIDTH=289><TT>void setEchoCharacter(char c)</TT></TD><TD WIDTH=301>Sets the object's echo character.</TD></TR><TR VALIGN=TOP><TD WIDTH=289><TT>void setText(String str)</TT></TD><TD WIDTH=301>Sets the text in the textfield object.</TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleUsingEchoCharacters">Example: Using Echo Characters</A></H3><P>You should already be pretty familiar with using textfield objectsin applets because you've used them already many times in previousapplets in this book. However, I've not yet mentioned echo characters,which enable you to create textfield objects that display a specialcharacter when the user types. The most common use for echo charactersis to set up text entry for things like passwords.<P>Listing 19.6 is the source code for a short applet called EchoAppletthat initializes a textfield object to use an asterisk as an echocharacter. Listing 19.7 is the applet's HTML document. When yourun the applet and type something in the textfield control, yousee the display in Figure 19.7. You can change the echo characterby clicking on the Change Echo button. Then, when you click onthe textfield control to enter text, the text in the control changesto the new echo character, as shown in Figure 19.8. The programswitches between three different echo characters: an asterisk(<TT>*</TT>), a pound sign (<TT>#</TT>), and a dollar sign (<TT>$</TT>).<P><A HREF="f19-7.gif"><B> Figure 19.7 : </B><I>When you start typing, you see asterisks instead of regular text characters.</I></A><P><P><A HREF="f19-8.gif"><B> Figure 19.8 : </B><I>When you click the Change Echo button, the applet switches to a different echo character.</I></A><P><HR><BLOCKQUOTE><B>Listing 19.6&nbsp;&nbsp;EchoApplet.java: Using Echo Characters.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class EchoApplet extends Applet{    TextField textField;    Button button;    public void init()    {        textField = new TextField(&quot;&quot;, 25);        button = new Button(&quot;Change Echo&quot;);        textField.setEchoCharacter('*');        add(textField);        add(button);    }    public boolean action(Event evt, Object arg)    {        if (evt.target instanceof Button)            ChangeEcho();        return true;    }    protected void ChangeEcho()    {        char c = textField.getEchoChar();        if (c == '*')            textField.setEchoCharacter('#');        else if (c == '#')            textField.setEchoCharacter('$');        else            textField.setEchoCharacter('*');                }}</PRE></BLOCKQUOTE><HR><P><IMG ALIGN=RIGHT SRC="pseudo.gif" HEIGHT=94 WIDTH=94 BORDER=1><BLOCKQUOTE>Tell Java that the applet uses the classes in the <TT>awt</TT>package.<BR>Tell Java that the applet uses the classes in the <TT>applet</TT>package.<BR>Derive the <TT>EchoApplet</TT> class from Java's <TT>Applet</TT>class.<BR>    Declare textfield and button objects.<BR>    Override the <TT>init()</TT> method.<BR>        Create the textfield and button objects.<BR>        Set the textfield object's echo character to an asterisk.<BR>        Add the textfield and button objects to the applet's display.<BR>    Override the <TT>action()</TT> method.<BR>        If the button caused the event, call the <TT>ChangeEcho()</TT>method.<BR>        Tell Java that the event was handled okay.<BR>    Declare the <TT>ChangeEcho()</TT> method.<BR>        Get the current echo character.<BR>        Reset the echo character to the next in the series.</BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 19.7&nbsp;&nbsp;ECHOAPPLET.htmL: EchoApplet's HTMLDocument.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>&lt;title&gt;Applet Test Page&lt;/title&gt;&lt;h1&gt;Applet Test Page&lt;/h1&gt;&lt;applet    code=&quot;EchoApplet.class&quot;    width=200    height=150    name=&quot;EchoApplet&quot;&gt;&lt;/applet&gt;</PRE></BLOCKQUOTE><HR><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>Because you've already had a lot of experience with textfield controls, you know that a textfield control generates an event when the user presses Enter after typing in the control. You can capture this event in the applet's <TT>action()</TT> method.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="Summary"><FONT SIZE=5 COLOR=#Ff0000>Summary</FONT></A></H2><P>In this chapter, you discovered two new controls that will helpyou build useful applets. Whenever you need the user to selectfrom a list of items, you can use checkbox controls, which canbe created in both nonexclusive and exclusive modes. TextFieldcontrols, on the other hand, enable the user to enter text datainto an applet. You can even disguise the user's entry by substitutingthe typed text with echo characters.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#Ff0000>Review Questions</FONT></A></H2><OL><LI>What are the three arguments required by the <TT>Checkbox</TT>class's constructor?<LI>What's another name for checkboxes that are set for exclusivemode?<LI>What are the two arguments needed by the <TT>TextField</TT>class's constructor?<LI>What method do you call in order to change the state of acheckbox?<LI>What is the difference between the nonexclusive and exclusivemodes for a checkbox control?<LI>What additional object do you need to group the items in acheckbox control?<LI>When would you use echo characters with a textfield control?<LI>How do you set a textfield control's echo character?<LI>How can you determine which checkbox in an applet generatedan event?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#Ff0000>Review Exercises</FONT></A></H2><OL><LI>Write an applet that displays a checkbox with the label <TT>Clickhere</TT>.<LI>Write an applet that displays a textfield control that displaysthe default text string <TT>Type here</TT>.<LI>Write an applet that contains one checkbox and textfield control.Change the checkbox's label to whatever the user types in thetextfield control.<LI>Write an applet that contains three checkboxes in nonexclusivemode.<LI>Revise the applet from exercise 4 so that the checkboxes operatein exclusive mode.</OL><HR><HR WIDTH="100%"></P></CENTER><!-- reference library footer #1--></CENTER><IMG SRC="/images/rule.gif" WIDTH="460" HEIGHT="5" VSPACE="5"ALT="Ruler image"><br><FONT SIZE="-1">Contact <a href="mailto:reference@developer.com">reference@developer.com</a> with questions or comments.<br><a href="/legal/">Copyright 1998</a> <a href="http://www.earthweb.com" target="_top">EarthWeb Inc.</a>,  All rights reserved.<BR>PLEASE READ THE <a href="/reference/usage.html">ACCEPTABLE USAGE STATEMENT</a>.<BR>Copyright 1998 Macmillan Computer Publishing. All rights reserved.</FONT></BLOCKQUOTE><!--outer table--><TD VALIGN="TOP"><!--right side ads --><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCAD1.REF" alt="Click here for more info"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCAD1.REF" alt="Click here for more info" height="88" width="88" border="0"></a><P><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCAD2.REF" alt="Click here for more info"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCAD2.REF" alt="Click here for more info" height="88" width="88" border="0"></a><P></td></tr></table></BODY></HTML>

⌨️ 快捷键说明

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