📄 ch21.htm
字号:
<HTML><HEAD><TITLE>developer.com - Reference</TITLE><META NAME="Author" CONTENT="developer.com"><META NAME="Keywords" CONTENT="javascript, ActiveX, VRML, Push, channel,push channel, applet, directory, news, community, developer, internet,intranet, java, java applet, J++, javabeans, java products, tools, javatools, JDK, java development kit, java development environment, JIT,JavaPlan, enterprise tools, JVM, Java Virtual Machine, Java resources, API,browser, java documentation, CDF, Common Desktop Format, RDF, ResourceDescription Framework, Netcaster, Netcast, Castanet, meta-language, metadata,solaris, Solaris, SUN, CGI, Perl, Oracle, database, network, Netscape API,Apache, apache, SSL, mSQL, Netscape, Fasttrack, FastTrack,industry standard, ActiveX bridge, web, internet, site, sitedesign, microsoft, active server pages, asp, iis, nt, win95, 95, html, cgi,frontpage, business, corporate, vbscript, javascript, activex, isapi,database, access, sql, server, www, 3.0, 3.01, 3.02, 3.03, 3.04, 4.0,4.0b2, 4.01, 4.03, 4.04, connection, online, commerce, publisher,publishing, publish, webmaster, dynamic html, dhtml, windows 98, netscape,netscape software, shareware, magazines, jobs, japan, free, freeware, freestuff"><META NAME="description" CONTENT="Developer.com is the leading onlineservice for developers. It includes Gamelan, the official directory for Java, as wellas news, information, tutorials, and directories for other Internet technologiesincluding ActiveX, JavaScript, perl, VRML, Java Beans, push technologies, andother Internet and intranet technologies. Developer.com is also the home ofDeveloper Direct, the Professional Developer's Store, where developers canpurchase and download thousands of Web, authoring and other developmentproducts at some of today's lowest prices."><META NAME="distribution" CONTENT="global"><META NAME="resource-type" CONTENT="document"><META NAME="language" CONTENT="en"></HEAD><BODY BACKGROUND="/images/curve_r-wide.gif" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#DD0000" VLINK="#999999" ALINK="#FF0000"><TABLE><TR><TD VALIGN=TOP WIDTH=120><A HREF="/" ><IMG SRC="/images/masthead_r.gif" WIDTH="100" HEIGHT="78" BORDER="0"ALT="Developer.com Logo"></a></TD><TD><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCBAN.REF/DT=null"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCBAN.REF/DT=null" HEIGHT="60" WIDTH="468" ALT="Click here to support our advertisers"><BR><CENTER>Click here to support our advertisers</CENTER></a></TD></TR></TABLE><!--outer table--><TABLE><!--outer table--><TR><!--outer table--><TD VALIGN="TOP"><!--navbar info --><table><tr><td><A HREF="http://www.developerdirect.com/" ><IMG SRC="/images/button_s.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>SHOPPING</b></font></a></td></tr><tr><td><A HREF="/community/jobs/c_jobs.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>JOB BANK</B></font></a></td></tr><tr><td><A HREF="http://www.classifieds2000.com/cgi-cls/display.exe?Developer+computer+searchcomputercategories"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>CLASSIFIEDS</B></font></a></td></tr><tr><td><A HREF="/directories/directories.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>DIRECTORIES</B></font></a></td></tr><tr><td><A HREF="/reference/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>REFERENCE</B></font></a><br><a href="/reference/r_library.html"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">Online Library</a></font></td></tr><tr><td><A HREF="/classroom/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>LEARNING CENTER</B></font></a></td></tr><tr><td><A HREF="/journal/"><IMG SRC="/images/button_n.gif" width="15" height="11" BORDER="0"><B><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">JOURNAL</font></B></a></td></tr><tr><td><A HREF="/news/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>NEWS CENTRAL</b></font></a></td></tr><tr><td><A HREF="/downloads/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>DOWNLOADS</b></font></a></td></tr><tr><td><A HREF="/community/" ><IMG SRC="/images/button_c.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>COMMUNITY</B></font></a></td></tr><tr><td><A HREF="/calendar/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>CALENDAR</b></font></a></td></tr><tr><td><A HREF="/about/"><IMG SRC="/images/button_a.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>ABOUT US</b></a></font></td></tr></table><table><tr><td><form action="/subscribe/list.cgi" method=post ><img src="/images/rule.gif" width="100" height="2"><br><B><FONT FACE="Arial, Helvetica" SIZE="-1">Journal:</b></font><br><input name='email' value='your e-mail' size="11"><input type='hidden' name='state' value='Subscribe'></td></tr><tr><td><input type='submit' value='Subscribe'><br><font size="-1">Get the weekly email highlights from the most popular journal for developers!<br><a href="http://www.developer.com/subscribe.html">Current issue</a><img src="/images/rule.gif" width="100" height="2"><input type='hidden' name='name' value='New Subscriber'></FORM></td></tr></table><!--/navbar--><font size="-1"><A HREF="http://www.developer.com">developer.com</a><BR><A HREF="http://www.developerdirect.com">developerdirect.com</a><BR><A HREF="http://www.htmlgoodies.com">htmlgoodies.com</a><BR><A HREF="http://www.javagoodies.com">javagoodies.com</a><br><A HREF="http://www.jars.com">jars.com</a><BR><a href="http://www.intranetjournal.com">intranetjournal.com</a><br><a href="http://www.javascripts.com">javascripts.com</a><br></font><!--outer table--></TD><!--outer table--><TD VALIGN="TOP"><BLOCKQUOTE><P><img src="/images/header_r.gif" width="440" height="50"><P><MAP NAME ="header_r2.gif.map"><AREA SHAPE ="RECT" HREF ="/reference/author.html" COORDS ="361,0,440,29"><AREA SHAPE ="RECT" HREF ="/reference/basic.html" COORDS ="254,0,360,29"><AREA SHAPE ="RECT" HREF ="/reference/help.html" COORDS ="183,0,251,29"><AREA SHAPE ="RECT" HREF ="/reference/cool.html" COORDS ="100,0,181,29"><AREA SHAPE ="RECT" HREF ="/reference/mybook.html" COORDS ="0,0,100,29"></MAP><MAP NAME ="header_r1.gif.map"><AREA SHAPE ="RECT" HREF ="/reference/personalize.html" COORDS ="339,0,439,29"></MAP><p><font face="Arial,Helvetica" size="-1" color="#006666"><A HREF="/reference/r_library.html"><B>All Categories</A> :</B><b><A HREF="/reference/r_java.html">Java</A></b></font><p><META><H1><FONT SIZE=6 COLOR=#Ff0000>Chapter 21</FONT></H1><H1><FONT SIZE=6 COLOR=#Ff0000>Scrollbar and Canvas Controls</FONT></H1><HR><P><CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER><UL><LI><A HREF="#Scrollbars">Scrollbars</A><UL><LI><A HREF="#ExampleCreatingaScrollbar">Example: Creating a Scrollbar</A><LI><A HREF="#RespondingtoaScrollbar">Responding to a Scrollbar</A><LI><A HREF="#ExampleUsingaScrollbarinanApplet">Example: Using a Scrollbar in an Applet</A><LI><A HREF="#Canvases">Canvases</A><LI><A HREF="#ExampleUsingaCanvasinanApplet">Example: Using a Canvas in an Applet</A></UL><LI><A HREF="#Summary">Summary</A><LI><A HREF="#ReviewQuestions">Review Questions</A><LI><A HREF="#ReviewExercises">Review Exercises</A></UL><HR><P>Next in the long list of Java controls are the scrollbar and canvascontrols, which are represented by the <TT>Scrollbar</TT> and<TT>Canvas</TT> classes. Using a scrollbar, you can enable theuser to choose values from a slider or even set up your own scrollingdisplays. The canvas control, on the other hand, enables you todraw and display graphics in an applet. In this chapter, you'llget a handle on both of these handy controls.<H2><A NAME="Scrollbars"><FONT SIZE=5 COLOR=#Ff0000>Scrollbars</FONT></A></H2><P>Practically every application that runs under Windows supportsscrollbars. In most cases, you use a scrollbar to move to differentportions of a document. However, a good alternative use for thesecontrols is selecting a value from a range. Although scrollbarsare not as common in applets, Java does include a class for addingscrollbars to your programs. To create a scrollbar, you firstcall the <TT>Scrollbar</TT> class's constructor, like this:<BLOCKQUOTE><PRE>Scrollbar scrollbar = new Scrollbar(orientation, start, page, min, max);</PRE></BLOCKQUOTE><P>The constructor's five arguments are the scrollbar's orientation(can be <TT>Scrollbar.HORIZONTAL</TT> or <TT>Scrollbar.VERTICAL</TT>),the starting setting for the scrollbar, the scrollbar's page size(the amount the display scrolls when the user clicks above orbelow the scroll box), and the minimum and maximum values representedby the scrollbar.<P>After creating the scrollbar object, you add it to the appletby calling the <TT>add()</TT> method, like this:<BLOCKQUOTE><PRE>add(scrollbar);</PRE></BLOCKQUOTE><H3><A NAME="ExampleCreatingaScrollbar">Example: Creating a Scrollbar</A></H3><P>Suppose you need to create a scrollbar that'll enable the userto select a value from 1 to 100. You can create such a scrollbarlike this:<BLOCKQUOTE><PRE>Scrollbar scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 50, 0, 1, 100);add(scrollbar);</PRE></BLOCKQUOTE><P>The constructor's first argument tells Java that the scrollbarshould be drawn horizontally on the display. The second argumenttells Java that you want the scrollbar to start off set to thevalue of 50. The third argument is the page size, which representsthe area in the slider covered by the scroll box. Finally, thefourth and fifth arguments give the scrollbar a minimum valueof 1 and a maximum value of 100.<P>These settings enable the user to select a value from 1 to 100.So, why is the scrollbar's page size set to zero? Doing this forcesthe scroll box in the scrollbar to center on the selected value.It also enables the user to select the maximum value of 100. Tounderstand why this is necessary, imagine that the scrollbar'sscroll box represents the page of data that's currently displayed(such as in a word processor document). The scroll box then startson the selected value and ends on the selected value plus thepage size. Figure 21.1 illustrates this concept. The scrollbarin the figure was created like this:<P><A HREF="f21-1.gif"><B> Figure 21.1 : </B><I>The scroll box covers an area from the selected value to the selected value plus the page size.</I></A><P><BLOCKQUOTE><PRE>Scrollbar scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 50, 10, 1, 100);</PRE></BLOCKQUOTE><P>Because the scrollbar's starting value is 50, and the scrollbar'spage size is 10, the scroll box covers the area of the sliderfrom 50 to 60. If the user were to drag the scroll box to itsmaximum value, the scroll box would cover the area in the sliderfrom 90 to 100, as shown in Figure 21.2. In this case, the pagesize of 10 makes it impossible for the user to actually selectthe value of 100. This is because, if the user could select thevalue 100, the scrollbox would have to cover the slider area from100 to 110. But, the scrollbar's maximum value is 100.<P><A HREF="f21-2.gif"><B> Figure 21.2 : </B><I>This maximum setting starts at 90 and goes to 90 plus the page size, which equals the maximum value <r>of 100.</I></A><P><P>When using a scrollbar to select specific values rather than areasof a document, you can either set the page size to zero or setthe maximum to the maximum you want plus the page size. Both ofthese scrollbars will operate the same way:<BLOCKQUOTE><PRE>Scrollbar scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 50, 10, 1, 110);Scrollbar scrollbar =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -