📄 ch22.htm
字号:
<P>This is where the applet creates its <TT>GridBagLayout</TT> objectand sets it as the applet's layout. In the next line, the appletcreates its <TT>GridBagConstraints</TT> object, like this:<BLOCKQUOTE><PRE>GridBagConstraints constraints = new GridBagConstraints();</PRE></BLOCKQUOTE><P>The applet will use this single <TT>GridBagConstraints</TT> objectin order to set the constraints for each component added to thelayout. Before components can be added, however, they must becreated, which the applet does as shown in Listing 22.7.<HR><BLOCKQUOTE><B>Listing 22.7 LST22_7.TXT: Creating the Applet'sButtons.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>Button button1 = new Button("Button1");Button button2 = new Button("Button2");Button button3 = new Button("Button3");Button button4 = new Button("Button4");Button button5 = new Button("Button5");Button button6 = new Button("Button6");Button button7 = new Button("Button7");Button button8 = new Button("Button8");Button button9 = new Button("Button9");</PRE></BLOCKQUOTE><HR><P>After creating the buttons, the program can start adding themto the layout. But before the first button gets added, the constraintsobject must contain the appropriate values. In this case, onlythe <TT>fill</TT> field must be initialized, since the first buttoncomponent will use all the other default values:<BLOCKQUOTE><PRE>constraints.fill = GridBagConstraints.BOTH;</PRE></BLOCKQUOTE><P>Setting the <TT>fill</TT> field to both ensures that the components(in this case, buttons) will expand both vertically and horizontallyto completely fill their display areas. After initializing theconstraints for the first button, the applet sets the constraintsand adds the button:<BLOCKQUOTE><PRE>layout.setConstraints(button1, constraints);add(button1);</PRE></BLOCKQUOTE><P>Now that you have the first button added, it's time to considerhow the second button will fit in the layout. The value the appletinitialized the <TT>fill</TT> field to will remain in effect forall buttons, so the applet doesn't need to change it again. However,the layout manager is going to want to know how <TT>button2</TT>should be placed. The following lines set the constraints andadd the button:<BLOCKQUOTE><PRE>constraints.gridwidth = GridBagConstraints.RELATIVE;layout.setConstraints(button2, constraints);add(button2);</PRE></BLOCKQUOTE><P>By setting <TT>gridwidth</TT> to <TT>GridBagConstraints.RELATIVE</TT>,the applet tells the layout manager that this button is the nextto the last component in this row, which will determine its width.The <TT>button3</TT> object is the last component for the firstrow, so it sets <TT>gridwidth</TT> to <TT>GridBagConstraints.REMAINDER</TT>:<BLOCKQUOTE><PRE>constraints.gridwidth = GridBagConstraints.REMAINDER;layout.setConstraints(button3, constraints);add(button3);</PRE></BLOCKQUOTE><P>The <TT>REMAINDER</TT> constant tells the layout manager thatthis control should fill the first row all the way to the end.<P>The <TT>button4</TT> component is the only object on its row,so it too uses a <TT>gridwidth</TT> of <TT>REMAINDER</TT>:<BLOCKQUOTE><PRE>constraints.gridwidth = GridBagConstraints.REMAINDER;layout.setConstraints(button4, constraints);add(button4);</PRE></BLOCKQUOTE><P>Yes, it's true that the first line above really isn't necessary,since <TT>gridwidth</TT> was already set to <TT>REMAINDER</TT>.However, I like leaving this kind of line around because it tellsme that I haven't forgotten something here, that I do indeed wanta width of <TT>REMAINDER</TT> for this button too.<P>Because <TT>button5</TT> is both the first and next-to-last buttonin its row, it uses a width of <TT>RELATIVE</TT>:<BLOCKQUOTE><PRE>constraints.gridwidth = GridBagConstraints.RELATIVE;layout.setConstraints(button5, constraints);add(button5);</PRE></BLOCKQUOTE><P>Because there's only two buttons in this row, the <TT>button6</TT>component gets a width of <TT>REMAINDER</TT>:<BLOCKQUOTE><PRE>constraints.gridwidth = GridBagConstraints.REMAINDER;layout.setConstraints(button6, constraints);add(button6);</PRE></BLOCKQUOTE><P>Now, things get a little tricky (like they weren't tricky enough,right?). If you look at Figure 22.11, you'll see that <TT>button7</TT>is one cell wide but two cells high. Buttons seven and eight,on the other hand are two cells wide but only one cell high. Eventhough <TT>button7</TT> is technically the next-to-last buttonin its row, you don't want to give it the <TT>RELATIVE</TT> widthbecause then Java will make the button twice as wide. So, theapplet sets the width of <TT>button7</TT> to 1 and the heightto 2, as shown in Listing 22.8.<HR><BLOCKQUOTE><B>Listing 22.8 LST22_8.TXT: Setting </B><I>button7's</I><B>size.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>constraints.gridwidth = 1;constraints.gridheight = 2;layout.setConstraints(button7, constraints);add(button7);</PRE></BLOCKQUOTE><HR><P>Now, because <TT>button8</TT> is the last button in its row, itgets the <TT>REMAINDER</TT> width. However, the button also mustbe set back to a normal one-cell height, as shown in Listing 22.9.<HR><BLOCKQUOTE><B>Listing 22.9 LST22_9.LST: Creating<I> </I></B><I>button8</I><B>.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>constraints.gridwidth = GridBagConstraints.REMAINDER;constraints.gridheight = 1;layout.setConstraints(button8, constraints);add(button8);</PRE></BLOCKQUOTE><HR><P>Finally, <TT>button9</TT> can use exactly the same restraints,which means simply setting the constraints and adding the button,like this:<BLOCKQUOTE><PRE>layout.setConstraints(button9, constraints);add(button9);</PRE></BLOCKQUOTE><P>All of the lines described in this section work together to createthe applet's layout. Every layout will work differently, requiringthat you carefully plan ahead how you want the applet's componentslaid out. There's almost an infinite number of ways to use theconstraints along with the <TT>GridBagLayout</TT> manager.<P>You may wonder how changes to this example layout will affectthe appearance of the applet. Suppose, for example, you left the<TT>fill</TT> field set to its default value of <TT>GridBagConstraints.NONE</TT>.You would then end up with a layout like that shown in Figure22.12. Figure 22.13 shows the applet with a <TT>fill</TT> settingof <TT>GridBagConstraints.VERTICAL</TT>.<P><A HREF="f22-13.gif"><B> Figure 22.13 : </B><I>The vertical fill stretches sone controls vertically.</I></A><P><P><A HREF="f22-12.gif"><B> Figure 22.12 : </B><I>The fill setting can make a huge difference in how a layout looks.</I></A><P><P>Another change you might make is to set <TT>weightx</TT> and <TT>weighty</TT>,which tells Java how to use the extra space that usually surroundsthe controls in the layout. For example, in <TT>GridBagApplet</TT>,if you set <TT>weightx</TT> to 1, you get a display like Figure22.14, because you've told Java that you want the layout to fillthe entire horizontal space in the applet. Setting <TT>weighty</TT>stretches the layout in the vertical direction, as shown in Figure22.15, which has both <TT>weightx</TT> and <TT>weighty</TT> set.<P><A HREF="f22-14.gif"><B> Figure 22.14 : </B><I>Setting the weightx field stretches the layout horizontally.</I></A><P><P><A HREF="f22-15.gif"><B> Figure 22.15 : </B><I>Setting both weightx and weighty stretches the layout in both directions.</I></A><P><H2><A NAME="Summary"><FONT SIZE=5 COLOR=#Ff0000>Summary</FONT></A></H2><P>Java gives you many options when it comes to creating a layoutfor the components that make up your applet. However, having somany possibilities at your fingertips can be daunting at first,because there may be several ways to get the effect that you want.The better you learn to use Java's layout managers, the more easilyyou'll know which is the appropriate manager for a specific situation.Mastering the <TT>GridBagLayout</TT> manager especially requirestime and patience.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#Ff0000>Review Questions</FONT></A></H2><OL><LI>Why would you add multiple <TT>Panel</TT> objects to an applet?<LI>What are the names of Java's five layout managers?<LI>Which layout manager is the default manager for an applet?<LI>How does the <TT>FlowLayout</TT> manager organize components?<LI>What are the arguments for the <TT>GridLayout</TT> manager'sconstructor?<LI>What component positions can use with a <TT>BorderLayout</TT>manager?<LI>How do you add a component to an applet that uses the <TT>BorderLayout</TT>manager?<LI>How does the <TT>CardLayout</TT> manager enable you to mimicWindows 95's property sheets?<LI>How do you switch from one card to another when using a <TT>CardLayout</TT>manager?<LI>How does a <TT>GridBagConstraints</TT> object work in conjunctionwith a <TT>GridBagLayout</TT> manager?<LI>What does the <TT>GridBagConstraints</TT>.fill field control?<LI>How do you add a component to a layout controlled by a <TT>GridBagLayout</TT>manager?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#Ff0000>Review Exercises</FONT></A></H2><OL><LI>Write an applet that uses a <TT>FlowLayout</TT> manager todisplay a button on the left side of the applet.<LI>Write an applet that displays nine buttons in a 3<TT>x</TT>3grid.<LI>Write an applet that uses a <TT>BorderLayout</TT> object todisplay a single button at the bottom of the applet.<LI>Write an applet that uses two panels to group two sets ofthree buttons.<LI>Write an applet called CardApplet2 that contains three pagesof controls. The first page should contain three buttons, thesecond page should contain a scrolling list, and the third pageshould contain a <TT>TextField</TT> control. Clicking on any controlshould cause the applet to switch to the next page (except forthe <TT>TextField</TT> control, which only sends an event whenyou press Enter). Figure 22.16 shows what CardApplet2 looks likewhen its scrolling list card is visible. (You can find the solutionto this exercise in the CHAP22 folder of this book's CD-ROM.)<BR><A HREF="f22-16.gif"><B> Figure 22.16 : </B><I>This is the second page of the CardApplet2 applet.</I></A><P><LI>Write an applet called GridBagApplet2 that displays threerows of buttons. The first row should contain two buttons, withthe first button twice as wide as the second. The second row shouldcontain two buttons, with the second button twice as wide as thefirst. The third row should contain three normal-width buttons.Figure 22.17 shows the completed applet running under Appletviewer.(You can find the solution for this exercise in the CHAP22 folderof this book's CD-ROM.)<BR><A HREF="f22-17.gif"><B> Figure 22.17 : </B><I>This is GridBagApplet2 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 + -