📄 ch26.htm
字号:
<P>When the applet runs, it has to find out what greeting to display.So, in the applet's <TT>init()</TT> method is the following line:<BLOCKQUOTE><PRE>String str = getParameter("greeting");</PRE></BLOCKQUOTE><P>Now that the applet has the text stored in the <TT>str</TT> variable,it can manipulate and display it any way it needs to.<H3><A NAME="ExampleUsingaParameterinanApplet">Example: Using a Parameter in an Applet</A></H3><P>Now that you know how to create HTML documents that set parameters,as well as how to obtain those parameters from within your applet,you'd probably like a real parameterized applet with which youcan experiment. Listing 26.2 is an applet called ConfigApplet,which takes a single parameter. This parameter is the text stringto display. Listing 26.3 is the HTML document that loads and runsthe applet. Notice the <TT><PARAM></TT> tag. When you runthe applet with Appletviewer, you see the window shown in Figure26.1.<P><A HREF="f26-1.gif"><B> Figure 26.1 : </B><I>This applet's display string is given as a parameter.</I></A><P><HR><BLOCKQUOTE><B>Listing 26.2 ConfigApplet.java: An Applet with aSingle Parameter.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class ConfigApplet extends Applet{ String str; public void init() { str = getParameter("text"); Font font = new Font("TimesRoman", Font.BOLD, 24); setFont(font); } public void paint(Graphics g) { g.drawString(str, 50, 50); }}</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>ConfigApplet</TT> class from Java's <TT>Applet</TT>class.<BR> Declare the class's data field.<BR> Override the <TT>init()</TT> method.<BR> Retrieve the value of the text parameter.<BR> Create and set the font for the applet.<BR> Override the <TT>paint()</TT> method.<BR> Display the given string.</BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 26.3 CONFIGAPPLET.htmL: HTML Document forConfigApplet.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE><title>Applet Test Page</title><h1>Applet Test Page</h1><applet code="ConfigApplet.class" width=250 height=150 name="ConfigApplet"> <PARAM NAME=text VALUE="Display Text"></applet></PRE></BLOCKQUOTE><HR><P>Once you get ConfigApplet compiled, try running the applet severaltimes, each time changing the parameter in the HTML document toa new text string. This will give you a good example of how parameterswork from the HTML document writer's point of view. Changing thevalue of the parameter in the HTML document is all you need todo to display a different text string. You don't have to changethe applet's source code at all.<H2><A NAME="MultipleParameters"><FONT SIZE=5 COLOR=#Ff0000>Multiple Parameters</FONT></A></H2><P>When you're writing an application that others may use in theirWeb pages, it's important that you make the applet as flexibleas possible. One way to do this is to use parameters for any appletvalue that the user might like to customize. Adding multiple parametersis just a matter of adding additional <TT><PARAM></TT> tagsto the HTML document and then retrieving the values of the parametersin the applet. In the next example, you take a look at ConfigApplet2,which gives the user much more control over how the applet displaysthe text string.<H3><A NAME="ExampleUsingMultipleParametersinanApplet">Example: Using Multiple Parameters in an Applet</A></H3><P>Suppose that you want to rewrite ConfigApplet so that the usercan customize not just the text string the applet will display,but also the position of the text and the size of the font usedto print the text. To do this, you need to create four parameters,one each for the text to display, the X position of the text,the Y position of the text, and the point size of the font. Listing26.4 is an HTML document that loads and runs the ConfigApplet2applet, which is a new version of ConfigApplet. Notice that theHTML document now specifies four parameters for the applet. Youcan specify as many parameters as you need for an applet.<HR><BLOCKQUOTE><B>Listing 26.4 CONFIGAPPLET2.htmL: HTML Document forConfigApplet2.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE><title>Applet Test Page</title><h1>Applet Test Page</h1><applet code="ConfigApplet2.class" width=350 height=200 name="ConfigApplet2"> <PARAM NAME=text VALUE="Display Text"> <PARAM NAME=typesize VALUE=72> <PARAM NAME=xpos VALUE=20>0 <PARAM NAME=ypos VALUE=100></applet></PRE></BLOCKQUOTE><HR><P>In the above HTML document, the user is specifying that he wantsto display the text string <TT>Display Text</TT> in 72-point typeand at position 20,100. The applet, of course, must call <TT>getParameter()</TT>to read these values into the applet. Moreover, the applet mustcall <TT>getParameter()</TT> once for each parameter. After retrievingthe parameters, the applet must initialize itself such that itdisplays the text as requested. Listing 26.5 is the Java sourcecode for ConfigApplet2, which accomplishes all these tasks. Figure26.2 shows the applet running under Appletviewer, using the parametersgiven in the HTML document in Listing 25.4.<P><A HREF="f26-2.gif"><B> Figure 26.2 : </B><I>This applet accepts four parameters that determine how the text is displayed.</I></A><P><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>Because the <TT>getParameter()</TT> method always returns a string, you may have to convert some parameters before you can use them in your applet. For example, the ConfigApplet2 applet must convert its <TT>typesize</TT>, <TT>xpos</TT>, and <TT>ypos</TT> parameters from strings to integers.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><HR><BLOCKQUOTE><B>Listing 26.5 ConfigApplet2.java: The ConfigApplet2Applet.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class ConfigApplet2 extends Applet{ String str; Point position; public void init() { String s; str = getParameter("text"); s = getParameter("typesize"); int typeSize = Integer.parseInt(s); s = getParameter("xpos"); int xpos = Integer.parseInt(s); s = getParameter("ypos"); int ypos = Integer.parseInt(s); position = new Point(xpos, ypos); Font font = new Font("TimesRoman", Font.BOLD, typeSize); setFont(font); } public void paint(Graphics g) { g.drawString(str, position.x, position.y); }}</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>ConfigApplet2</TT> class from Java's <TT>Applet</TT>class.<BR> Declare the class's data fields.<BR> Override the <TT>init()</TT> method.<BR> Declare a local variable.<BR> Retrieve the value of the text parameter.<BR> Retrieve and convert the typesize parameter.<BR> Retrieve and convert the xpos parameter.<BR> Retrieve and convert the ypos parameter.<BR> Store the position in a Point object.<BR> Create and set the font for the applet, using typesize.<BR> Override the <TT>paint()</TT> method.<BR> Display the given string.</BLOCKQUOTE><P>Suppose you were to change the parameters in the HTML file tothose shown in Listing 26.6. You'd then completely change theway the text string is displayed in the applet, as shown in Figure26.3. As you can see, parameters can have a profound effect onthe way an applet looks and acts.<P><A HREF="f26-3.gif"><B> Figure 26.3 : </B><I>Here's ConfigApplet2 running with different parameters.</I></A><P><HR><BLOCKQUOTE><B>Listing 26.6 LST26_6.TXT: New Parameters for ConfigApplet2.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE><PARAM NAME=text VALUE="New Text String">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -