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

📄 ch3.htm

📁 JAVA Developing Professional JavaApplets
💻 HTM
📖 第 1 页 / 共 5 页
字号:
so are set comfortably at the top and bottom of the applet. Theremaining display is then used for the <TT>&quot;Center&quot;</TT>component, the DrawCanvas object. This object takes up the bulkof the applet display area.<P>That the BorderLayout class can display up to only five components(corresponding to the five directions) might concern users; however,this is not really a problem. Recall that since containers canhold other containers, this limitation does not really exist.Consequently, an applet can contain Panel objects, which in turncan contain other containers, and so forth.<P>In the example, the Panel class is used to align the button andchoice box on the same row. The Panel class can be used as a kindof &quot;toolbar,&quot; displaying components along the top orbottom of the screen. It exemplifies this behavior because itsdefault layout is the FlowLayout class (see the following section).<H4>Flow Layout</H4><P>The FlowLayout class is good for displaying components horizontallyacross the container. It is the default layout for Panels, whichcan be set to function like a toolbar or to contain related Components,such as OK and Cancel buttons. In most cases, the FlowLayout classwill present components across a single row. However, if the componentsdo not fit on one row, a new row is started.<P>Figure 3.3 illustrates a modification of Listing 3.1, which addsanother Choice menu to the left of the button (whose text hasbeen modified). The canvas uses this new Choice object to pickthe color used to paint the object chosen in the other Choicemenu. Since the order of when a component is added to a containeris important in determining the layout display, the color Choiceobject is added before the button.<P><A HREF="f3-3.gif" ><B>Figure 3.3 </B>: <I>Second version, illustrating FlawLayout</I></A><I>.</I><P>The following code modifies the <TT>init()</TT>method in Listing 3.1 to build the Panel object shown in Figure3.4. It is inserted in the source code after the DrawCanvas objectis created, but before the Button is made.<BLOCKQUOTE><TT>// Create the panel with button and choicesat <BR>bottom...<BR>Panel p = new Panel();<BR>// Explicitly set to flow layout...<BR>p.setLayout(new FlowLayout());<BR>// Add color choice to left of button...<BR>colorChoice = new Choice();<BR>colorChoice.addItem(&quot;Yellow&quot;);<BR>colorChoice.addItem(&quot;Red&quot;);<BR>colorChoice.addItem(&quot;Blue&quot;);<BR>p.add(colorChoice);<BR>// Add button...</TT></BLOCKQUOTE><P>The code explicitly creates a new FlowLayout object for the Panelto use. This is unnecessary, but it illustrates how a FlowLayoutobject could be set up for Container objects in general.<P>To use the color Choice menu in the DrawCanvas code, add a methodto the Example1 class to return the color selection:<BLOCKQUOTE><TT>// Get the color to be displayed....<BR>// Convert the String to a Color object<BR>Color getColor() {<BR> String s = colorChoice.getSelectedItem();<BR> if (s.compareTo(&quot;Yellow&quot;) == 0)<BR>&nbsp;&nbsp;return Color.yellow;<BR> if (s.compareTo(&quot;Red&quot;) == 0)<BR>&nbsp;&nbsp;return Color.red;<BR> return Color.blue;<BR>}</TT></BLOCKQUOTE><P>The DrawCanvas class modifies its color code to get the colorchoice from the applet class:<BLOCKQUOTE><TT>g.setColor(e1app.getColor());</TT></BLOCKQUOTE><P>You can specify other features of FlowLayout to customize itsappearance. By default, components within a container using FlowLayoutare aligned along the center. However, alternative FlowLayoutconstructors can be used to align the components to the left orthe right. Figure 3.4 shows how the Panel in the example wouldlook if it were right-aligned. The following code line is allyou need to add to modify how the layout is established:<BLOCKQUOTE><TT>p.setLayout(new FlowLayout(FlowLayout.RIGHT));</TT></BLOCKQUOTE><P>You can specify the number of pixels between the components ina container using FlowLayout in an alternative constructor. Thisdifference in the spacing between the components is known as the<I>gap value</I>. Both the horizontal and vertical gap valuescan be set in FlowLayout. Most of the LayoutManager classes supportsetting gaps. The discussion of the next layout, GridLayout, willillustrate how to use gap values.<P><A HREF="f3-4.gif" ><B>Figure 3.4 </B>: <I>The FlawLayout panel is right-justified.</I></A><H4>GridLayout</H4><P>The GridLayout class is used to set a matrix of components alonga number of rows and columns. Since the size of each row and columnis the same, each component in the grid has the same size. Eachnew component added to the container using GridLayout is positionedto the next index in the grid. If the row is not full, it is addedto the next column; if it is, a new row is started and the componentis added to the new column.<P>Figure 3.5 illustrates an applet set to use GridLayout. It isa 5<FONT FACE="Symbol">&#165;</FONT>5 matrix of Buttons. Listing3.2 shows the code used to create this applet and illustratesthe use of gap values. In this case, a horizontal gap of 10 anda vertical gap of 20 is specified in the constructor.<HR><BLOCKQUOTE><B>Listing 3.2. Creating an example using GridLayout</B>.<BR></BLOCKQUOTE><BLOCKQUOTE><TT>import java.awt.*;<BR>import java.lang.*;<BR>import java.applet.*;<BR><BR>// Class used for illustrating Grid Layouts...<BR>public class GridLayoutExample extends Applet&nbsp;&nbsp;{<BR>&nbsp;// Set up a matrix of numbers to be displayed in a <BR>grid...<BR>&nbsp;public void init() {<BR>&nbsp;&nbsp;// Set up display area...<BR>&nbsp;&nbsp;resize(300,200);<BR>&nbsp;&nbsp;// Set the layout to a 5 by 5 grid with<BR>&nbsp;&nbsp;// a horizontal gap of 10 and a vertical gap of 20<BR>&nbsp;&nbsp;int rowsAcross = 5;<BR>&nbsp;&nbsp;int rowsDown = 5;<BR>&nbsp;&nbsp;setLayout(new GridLayout(rowsAcross,rowsDown,10,20));<BR>&nbsp;&nbsp;// Fill the grid with buttons filled with numbers...<BR>&nbsp;&nbsp;int matrixSize = rowsAcross *rowsDown;<BR>&nbsp;&nbsp;for (int i = 0; i &lt; matrixSize; ++i) {<BR>&nbsp;&nbsp;&nbsp;// Make a label set to the current number...<BR>&nbsp;&nbsp;&nbsp;// Add it to the grid...<BR>&nbsp;&nbsp;&nbsp;add(new Button(Integer.toString(i)) );<BR>&nbsp;&nbsp;}<BR></TT>&nbsp;<TT>}<BR>}</TT></BLOCKQUOTE><HR><P><A HREF="f3-5.gif" ><B>Figure 3.5 </B>: <I>An example using GridLayout.</I></A><P>The code ends with a <TT>for</TT>loop that adds Buttons to the grid. As the number increases, eachButton is added across and down the applet display area. The codethat creates the numeric name of the Button is interesting becauseit illustrates a static method of the type wrapper class, Integer,that can be used to convert an integer to a String without creatinga new object.<P>An alternative constructor enables you to create a GridLayoutwithout horizontal and vertical gaps.<H4>GridBagLayout</H4><P>The most complex layout class provided with the Java API is GridBagLayout.While it is superficially similar to the GridLayout class, itdiffers significantly by not requiring the components in the gridto be the same size. GridBagLayout uses a helper class calledGridBagConstraints to specify how the component is displayed inrelation to the container's other components. GridBagLayout canguarantee a logical display of components because it replacesthe use of hard-code coordinates with a relative structure ofhow the components should visually interrelate.<P>Figure 3.6 and Listing 3.3 show an example of using GridBagLayout.As the code illustrates, this class is much more complex thanthe other layouts. The key to using GridBagLayout is understandingits interaction with the GridBagConstraints helper. To describeit in high-level terms, GridBagLayout and GridBagConstraints usea system of weighting and relative flags to determine how thingswill be positioned and sized. To see how this works, look at someof the GridBagConstraints variables summarized in Table 3.1. Thevariables starting with &quot;grid&quot; specify positioning inrelation to the other components in the row or column. The GridBagConstraintsconstant <TT>REMAINDER</TT> meansthat the object should be the last item in the row or column.A value of <TT>1</TT>, on the otherhand, indicates that it should be positioned normally. The <TT>weightx</TT>and <TT>weighty</TT> variables determinespace distribution of the components in relation to each other.A weight of <TT>1</TT> indicates thatthe item should be positioned evenly with other items of weight<TT>1</TT>. On the other hand, a weightof <TT>0</TT> will give a componenta lower priority in sizing. If the weight variables are not setto a nonzero value, the default distribution will be moved towardthe center of the container.<P><A HREF="f3-6.gif" ><B>Figure 3.6 </B>: <I>An example using GridBagLayout.</I></A><HR><BLOCKQUOTE><B>Listing 3.3. Creating an example using GridBagLayout.<BR></B></BLOCKQUOTE><BLOCKQUOTE><TT>import java.awt.*;<BR>import java.lang.*;<BR>import java.applet.*;<BR><BR>// Class used for illustrating GridBagLayouts...<BR>public class GridBagLayoutExample extends Applet&nbsp;&nbsp;{<BR><BR></TT>&nbsp;<TT>// A complex set ofbuttons<BR>&nbsp;public void init() {<BR>&nbsp;&nbsp;// Just reuse these over &amp; over...<BR>&nbsp;&nbsp;Button b;<BR>&nbsp;&nbsp;Label l;<BR>&nbsp;&nbsp;// Set up display area...<BR>&nbsp;&nbsp;resize(300,200);<BR>&nbsp;&nbsp;// Create the GridBagLayout and its helper...<BR>&nbsp;&nbsp;GridBagLayout g = new GridBagLayout();<BR>&nbsp;&nbsp;setLayout(g);<BR>&nbsp;&nbsp;GridBagConstraints gbc = new GridBagConstraints();<BR>&nbsp;&nbsp;// *************************************<BR>&nbsp;&nbsp;// Put up a row of three equal size buttons...<BR>&nbsp;&nbsp;// *************************************<BR>&nbsp;&nbsp;// This tells the layout to use the full horizontal<BR>&nbsp;&nbsp;// and vertical height if the display area is not<BR>filled...<BR></TT>&nbsp;<TT>&nbsp;gbc.fill = GridBagConstraints.BOTH;<BR>&nbsp;&nbsp;// Distribute horizontal space evenly between buttons<BR>&nbsp;&nbsp;gbc.weightx = 1.0;<BR>&nbsp;&nbsp;// Create and add the three buttons...<BR>&nbsp;&nbsp;b = new Button(&quot;Number 1&quot;);<BR>&nbsp;&nbsp;g.setConstraints(b,gbc);<BR>&nbsp;&nbsp;add(b);<BR>&nbsp;&nbsp;b = new Button(&quot;Number 2&quot;);<BR>&nbsp;&nbsp;g.setConstraints(b,gbc);<BR>&nbsp;&nbsp;add(b);<BR>&nbsp;&nbsp;b = new Button(&quot;Number 3&quot;);<BR>&nbsp;&nbsp;gbc.gridwidth = GridBagConstraints.REMAINDER; // Fill<BR>up the row...<BR></TT>&nbsp;<TT>&nbsp;g.setConstraints(b,gbc);<BR>&nbsp;&nbsp;add(b);<BR>&nbsp;&nbsp;// *************************************<BR>&nbsp;&nbsp;// Put up a button, a label, and a button<BR>&nbsp;&nbsp;// that uses the remaining height area...<BR>&nbsp;&nbsp;// *************************************<BR>&nbsp;&nbsp;b = new Button(&quot;Number 4&quot;);<BR>&nbsp;&nbsp;gbc.gridwidth = 1;&nbsp;&nbsp;// Reset to normal...<BR>&nbsp;&nbsp;gbc.weighty = 1.0;&nbsp;&nbsp;// Force it to use remaining<BR>height...<BR>&nbsp;&nbsp;g.setConstraints(b,gbc);<BR>&nbsp;&nbsp;add(b);<BR>&nbsp;&nbsp;l = new Label(&quot;Number 5&quot;);<BR>&nbsp;&nbsp;g.setConstraints(l,gbc);<BR>&nbsp;&nbsp;add(l);<BR>&nbsp;&nbsp;b = new Button(&quot;Number 6&quot;);<BR>&nbsp;&nbsp;gbc.gridwidth = GridBagConstraints.REMAINDER; // Fill<BR>up the row...<BR></TT>&nbsp;<TT>&nbsp;g.setConstraints(b,gbc);<BR>&nbsp;&nbsp;add(b);<BR></TT>&nbsp;<TT>&nbsp;// *************************************<BR>&nbsp;&nbsp;// Make a normal height button with insets...<BR>&nbsp;&nbsp;// *************************************<BR>&nbsp;&nbsp;gbc.weighty = 0.0; // Normal height;<BR>&nbsp;&nbsp;gbc.gridheight = 1;<BR>&nbsp;&nbsp;gbc.weightx = 0.0;&nbsp;&nbsp;// Use up the row...<BR>&nbsp;&nbsp;gbc.insets.left = 20;<BR>&nbsp;&nbsp;gbc.insets.right = 20;<BR>&nbsp;&nbsp;b = new Button(&quot;Number 7&quot;);<BR>&nbsp;&nbsp;g.setConstraints(b,gbc);<BR>&nbsp;&nbsp;add(b);<BR>&nbsp;&nbsp;// *************************************<BR>&nbsp;&nbsp;// Finally add a text field across the bottom...<BR>&nbsp;&nbsp;// *************************************<BR>&nbsp;&nbsp;gbc.insets.left = 0;&nbsp;&nbsp;&nbsp;// Reset these...<BR>&nbsp;&nbsp;gbc.insets.right = 0;<BR>&nbsp;&nbsp;TextField t = new TextField(&quot;Number 8&quot;);<BR>&nbsp;&nbsp;g.setConstraints(t,gbc);<BR>&nbsp;&nbsp;add(t);<BR>&nbsp;}<BR><BR>}</TT></BLOCKQUOTE><HR><P><CENTER><B>Table 3.1. The GridBagConstraints variables.</B></CENTER><P><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=163><I>Variables</I></TD><TD WIDTH=427><I>Description</I>

⌨️ 快捷键说明

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