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

📄 ch21.htm

📁 Java_by_Example,初级经典例子哦,珍藏版本
💻 HTM
📖 第 1 页 / 共 3 页
字号:
to the &quot;North&quot; position, you get a scrollbar the stretchesacross the top of the applet. Failure to place the scrollbar properlyin an appropriate layout manager will result in a useless scrollbarlike the one shown in Figure 21.5.<P><A HREF="f21-5.gif"><B> Figure 21.5 : </B><I>The scrollbar in this applet was not placed properly in an appropriate layout manager.</I></A><P><H3><A NAME="Canvases">Canvases</A></H3><P>Canvases are nothing more than areas on which you can draw. Youcan combine canvases with other types of components, such as buttons,in order to build layouts that that are attractive, as well asfunctional. The first step in creating a canvas is to call the<TT>Canvas</TT> class's constructor, like this:<BLOCKQUOTE><PRE>Canvas canvas = new Canvas();</PRE></BLOCKQUOTE><P>The <TT>Canvas</TT> constructor requires no arguments.<P>Once you have the canvas created, you add it to your layout justas you would any other component, by calling the <TT>add()</TT>method:<BLOCKQUOTE><PRE>add(canvas);</PRE></BLOCKQUOTE><H3><A NAME="ExampleUsingaCanvasinanApplet">Example: Using a Canvas in an Applet</A></H3><P>To end this chapter, take a look at Listing 21.2, which is anapplet that creates a canvas class and uses the class to displaya colored area on the screen. When you run the applet with Appletviewer,you see the window shown in Figure 21.6. The applet displays twocomponents: a button at the top of the applet and a canvas belowthe button. When you click the button, the canvas changes color.<P><A HREF="f21-6.gif"><B> Figure 21.6 : </B><I>This is CanvasApplet running under Appletviewer.</I></A><P><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>Often, you'll want to derive your own custom canvas class from Java's <TT>Canvas</TT> class. Then, you can more easily control what's drawn in the canvas, by overriding the canvas's <TT>paint()</TT> method. This is the approach that's used in the CanvasApplet applet.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><HR><BLOCKQUOTE><B>Listing 21.2&nbsp;&nbsp;CanvasApplet.java: An Applet That Displaysa Canvas.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class CanvasApplet extends Applet{    CustomCanvas canvas;    public void init()    {        setLayout(new BorderLayout());        Button button = new Button(&quot;Color&quot;);        add(&quot;North&quot;, button);        canvas = new CustomCanvas();        add(&quot;Center&quot;, canvas);        resize(200, 250);    }    public boolean action(Event evt, Object arg)    {        if (arg == &quot;Color&quot;)            canvas.swapColor();        return true;    }}class CustomCanvas extends Canvas{    Color color;    public CustomCanvas()    {        color = Color.black;    }    public void paint(Graphics g)    {        Rectangle r = bounds();        g.setColor(color);        g.fillRect(0, 0, r.width, r.height);        g.setColor(Color.white);        g.drawString(&quot;CANVAS&quot;, 72, 90);    }    public void swapColor()    {        if (color == Color.black)            color = Color.red;        else if (color == Color.red)            color = Color.green;        else            color = Color.black;        repaint();    }}</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>CanvasApplet</TT> class from Java's <TT>Applet</TT>class.<BR>    Declare the custom canvas object.<BR>    Override the <TT>init()</TT> method.<BR>        Create and set the layout.<BR>        Create and add the button.<BR>        Create and add the canvas.<BR>        Size the applet.<BR>    Override the <TT>action()</TT> method.<BR>        if the &quot;Color&quot; button was pressed...<BR>            Tell the canvas to change color.<BR>        Tell Java that the event was handled.<BR>Define the <TT>CustomCanvas</TT> class.<BR>    Declare the class's single data field.<BR>    Declare the class's constructor.<BR>        Set the initial canvas color.<BR>    Override the <TT>paint()</TT> method.<BR>        Get the canvas's size.<BR>        Set the currently selected color.<BR>        Fill the canvas with the selected color.<BR>        Set the color for the text.<BR>        Display the text string.<BR>    Define the <TT>swapColor()</TT> method.<BR>        Set color to the next color.<BR>        Repaint the canvas with the new color.</BLOCKQUOTE><H2><A NAME="Summary"><FONT SIZE=5 COLOR=#Ff0000>Summary</FONT></A></H2><P>Most often, you see scrollbars being used to manipulate such controlsas scrolling lists and text areas. However, scrollbars also enableyou to create a reasonable facsimile of a Windows 95 slider control,which enables the user to select a value from a given range. Rememberthat you need to use the appropriate layout manager in order forthe scrollbar to be drawn properly. Unlike scrollbars, canvasesaren't usually used to obtain input from the user (although theydo generate some types of event messages). Instead, canvases enableyou to create graphical areas in your applets.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#Ff0000>Review Questions</FONT></A></H2><OL><LI>What are the <TT>Scrollbar</TT> constructor's five arguments?<LI>What is a canvas?<LI>What arguments are expected by the <TT>Canvas</TT> class'sconstructor?<LI>With a scrollbar, when would you use a page size of zero?<LI>What's the easiest way to respond to a scrollbar change?<LI>What are the five event messages that are generated by a scrollbar?<LI>How can you create a custom canvas component?<LI>How do you draw in a custom canvas?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#Ff0000>Review Exercises</FONT></A></H2><OL><LI>Write an applet that displays a vertical scrollbar.<LI>Write an applet that displays a scrollbar along with threebuttons. Clicking the buttons should set the scrollbar's valuesto its minimum, middle, and maximum values. (Hint: You can setthe scrollbar's value with its <TT>setValue()</TT> method.)<LI>Modify CanvasApplet so that the display includes, besidesthe canvas, three buttons labeled &quot;Black,&quot; &quot;Green,&quot;and &quot;Red.&quot; Clicking a button should change the canvasto the appropriate color. You'll probably want to use a <TT>GridLayout</TT>object for this applet's layout manager. (For more informationon using a <TT>GridLayout</TT> object, see <A HREF="ch22.htm" >Chapter 22</A>, &quot;Panelsand the Layout Manager.&quot; Figure 21.7 shows the resultantapplet. (You can find the solution to this exercise in the CHAP21folder of this book's CD-ROM. It's called CanvasApplet2.)<BR><A HREF="f21-7.gif"><B> Figure 21.7 : </B><I>This is CanvasApplet2 running under Appletviewer.</I></A><P></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 + -