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

📄 ch22.htm

📁 Java_by_Example,初级经典例子哦,珍藏版本
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<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&nbsp;22</FONT></H1><H1><FONT SIZE=6 COLOR=#Ff0000>Panels and Layout Managers</FONT></H1><HR><P><CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER><UL><LI><A HREF="#Panels">Panels</A><UL><LI><A HREF="#ExampleCreatingandUsingPanels">Example: Creating and Using Panels</A></UL><LI><A HREF="#LayoutManagers">Layout Managers</A><LI><A HREF="#TheIFlowLayoutIManager">The <I>FlowLayout</I> Manager</A><UL><LI><A HREF="#ExampleCreatingaIFlowLayoutIManager">Example: Creating a <I>FlowLayout</I> Manager</A></UL><LI><A HREF="#TheIGridLayoutIManager">The <I>GridLayout</I> Manager</A><UL><LI><A HREF="#CreatingaIGridLayoutIManager">Creating a <I>GridLayout</I> Manager</A></UL><LI><A HREF="#TheIBorderLayoutIManager">The <I>BorderLayout</I> Manager</A><UL><LI><A HREF="#CreatingaIBorderLayoutIManager">Creating a <I>BorderLayout</I> Manager</A></UL><LI><A HREF="#TheICardLayoutIManager">The <I>CardLayout</I> Manager</A><UL><LI><A HREF="#TheICardLayoutIManagerMethods">The <I>CardLayout</I> Manager Methods</A><LI><A HREF="#ExampleCreatingaICardLayoutIManager">Example: Creating a <I>CardLayout</I> Manager</A></UL><LI><A HREF="#TheIGridBagLayoutIManager">The <I>GridBagLayout</I> Manager</A><UL><LI><A HREF="#CreatingandSettingtheIGridBagLayoutIManager">Creating and Setting the <I>GridBagLayout</I> Manager</A><LI><A HREF="#CreatingandSettingaIGridBagConstraintsIObject">Creating and Setting a <I>GridBagConstraints</I> Object</A><LI><A HREF="#ExampleUsingaIGridBagLayoutIManagerinanApplet">Example: Using a <I>GridBagLayout</I> Manager in an Applet</A><LI><A HREF="#UnderstandingtheIGridBagAppletIApplet">Understanding the <I>GridBagApplet</I> 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>Up until the previous chapter, when you've added controls to yourapplets, you've let Java place those controls wherever it feltlike it. The only way you could control positioning was by changingthe size of the applet. Obviously, if you're going to produceattractive applets that are organized logically, you need someway to tell Java exactly where you want things placed. Java'slayout managers were created for exactly this purpose. Workingin conjunction with layout managers are components called panels,which enable you to organize other applet components. In thischapter, you learn about these two important layout components.<H2><A NAME="Panels"><FONT SIZE=5 COLOR=#Ff0000>Panels</FONT></A></H2><P>A panel is a special type of container object that acts as a parentto other components that you want to organize in your applet.For example, you can add several panels to an applet, each withtheir own layout. By using panels in this way, you can createmany different creative displays. Creating a panel is as easyas calling the <TT>Panel</TT> class's constructor, like this:<BLOCKQUOTE><PRE>Panel panel = new Panel();</PRE></BLOCKQUOTE><P>As you can see, the <TT>Panel</TT> class's constructor requiresno arguments.<P>Once you create a panel, you add it to the applet in the normalway, by calling the <TT>add()</TT> method:<BLOCKQUOTE><PRE>add(panel);</PRE></BLOCKQUOTE><H3><A NAME="ExampleCreatingandUsingPanels">Example: Creating and Using Panels</A></H3><P>Using panels can be a little confusing at first, so an exampleis in order. Suppose you need to create an applet that displaysfour buttons, but you don't want Java to place the buttons oneafter the other in the display, which Java will do with its defaultlayout. Instead, you want the buttons displayed in two rows oftwo. One way to accomplish this feat is to add two panels to theapplet and then add two buttons to each panel. Listing 22.1 showshow this is done, whereas Figure 22.1 shows what the display lookslike.<P><A HREF="f22-1.gif"><B> Figure 22.1 : </B><I>Using panels, <r>you can more easily organize components in an applet.</I></A><P><HR><BLOCKQUOTE><B>Listing 22.1&nbsp;&nbsp;PanelApplet.java: Using Panels.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class PanelApplet extends Applet{    Panel panel1, panel2;    Button button1, button2, button3, button4;    public void init()    {        panel1 = new Panel();        panel2 = new Panel();        add(panel1);        add(panel2);        button1 = new Button(&quot;Button1&quot;);        button2 = new Button(&quot;Button2&quot;);        button3 = new Button(&quot;Button3&quot;);        button4 = new Button(&quot;Button4&quot;);        panel1.add(button1);        panel1.add(button2);        panel2.add(button3);        panel2.add(button4);    }}</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>PanelApplet</TT> class from Java's <TT>Applet</TT>class.<BR>    Declare the panel and button objects.<BR>    Override the <TT>init()</TT> method.<BR>        Create the panels.<BR>        Add the panels to the applet.<BR>        Create the four buttons.<BR>        Add the buttons to the panels.</BLOCKQUOTE><P>Notice how, when adding the panels to the applet, the programcalls the <TT>PanelApplet</TT> class's <TT>add()</TT> method (whichadds the panels to the applet's display). However, when addingthe buttons, the program calls the panel objects' <TT>add()</TT>method (which adds the buttons to the panels). This is how youbuild a hierarchy of components into your applets. In this case,you've got a stack of components three high, with the applet'sdisplay on the bottom, the two panels on top of that, and thefour buttons on top of the panels. As you create more sophisticatedapplets, this type of component stacking will be more common.<P>Panels are kind of a &quot;plain vanilla&quot; container for organizingcomponents in an applet. As you'll discover in the next section,you can combine panels with layout managers to create truly complexdisplays.<H2><A NAME="LayoutManagers"><FONT SIZE=5 COLOR=#Ff0000>Layout Managers</FONT></A></H2><P>Layout Managers are special objects that determine how elementsof your applet are organized in the applet's display. When youcreate an applet, Java automatically creates and assigns a defaultlayout manager. In many of the applets you've created so far inthis book, it's the default layout manager that's determined whereyour controls appear. You can, however, create different typesof layout managers in order to better control how your appletslook. The layout managers you can use are listed below:<UL><LI>FlowLayout<LI>GridLayout<LI>BorderLayout<LI>CardLayout<LI>GridBagLayout</UL><P>Each of these layout managers is represented by a class of thesame name. To create a layout manager for your applet, you firstcreate an instance of the appropriate layout class and then callthe <TT>setLayout()</TT> method to tell Java which layout objectyou want to use. In the following sections, you get a chance tosee the various layout managers in action.<H2><A NAME="TheIFlowLayoutIManager"><FONT SIZE=5 COLOR=#Ff0000>The <I>FlowLayout</I> Manager</FONT></A></H2><P>

⌨️ 快捷键说明

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