📄 ch22.htm
字号:
In the previous section, I mentioned that, when you create anapplet, Java assigns to it a default layout manager. It just sohappens that this default manager is an object of the <TT>FlowLayout</TT>class. The <TT>FlowLayout</TT> manager places controls, in theorder in which they're added, one after the other in horizontalrows. When the layout manager reaches the right border of theapplet, it begins placing controls on the next row. In its defaultstate, the <TT>FlowLayout</TT> manager centers controls on eachrow. However, you can set the alignment when you create the layoutmanager for your applet, like this:<BLOCKQUOTE><PRE>FlowLayout layout = new FlowLayout(align, hor, ver);SetLayout(layout);</PRE></BLOCKQUOTE><P>The <TT>FlowLayout</TT> constructor takes three arguments, whichare the alignment (<TT>FlowLayout.LEFT</TT>, <TT>FlowLayout.CENTER</TT>,or <TT>FlowLayout.RIGHT</TT>), the horizontal spacing betweencomponents, and the vertical spacing.<H3><A NAME="ExampleCreatingaIFlowLayoutIManager">Example: Creating a <I>FlowLayout</I> Manager</A></H3><P>Suppose that you want to arrange three buttons in an applet usinga <TT>FlowLayout</TT> manager set to left alignment. Listing 22.2shows how you'd create the manager and the buttons for the applet.Figure 22.2 shows the resultant control layout. Figures 22.3,and 22.4 show the center and right alignments for the same controls.<P><A HREF="f22-2.gif"><B> Figure 22.2 : </B><I>These buttons are left aligned by the FlowLayout manager.</I></A><P><P><A HREF="f22-3.gif"><B> Figure 22.3 : </B><I>These buttons are center aligned by the FlowLayout manager.</I></A><P><P><A HREF="f22-4.gif"><B> Figure 22.4 : </B><I>These buttons are right aligned by the FlowLayout manager.</I></A><P><HR><BLOCKQUOTE><B>Listing 22.2 LST22_2.TXT: Creating a </B><I>FlowLayout</I><B>Manager.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>FlowLayout layout = new FlowLayout(FlowLayout.LEFT, 10, 10);setLayout(layout);button1 = new Button("Button1");button2 = new Button("Button2");button3 = new Button("Button3");add(button1);add(button2);add(button3);</PRE></BLOCKQUOTE><HR><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>The <TT>FlowLayout()</TT> constructor shown in this chapter takes four arguments. However, you can actually construct a <TT>FlowLayout</TT> object with no arguments, <TT>FlowLayout()</TT>, or with a single argument for the alignment, <TT>FlowLayout(FlowLayout.LEFT)</TT>. Many of Java's classes have multiple constructors.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="TheIGridLayoutIManager"><FONT SIZE=5 COLOR=#Ff0000>The <I>GridLayout</I> Manager</FONT></A></H2><P>Once you start creating more sophisticated applets, you'll quicklydiscover that the <TT>FlowLayout</TT> manager may not give youthe control you need to create the kind of display you want foryour applet. When you need more control over the placement ofcomponents, you can try out the <TT>GridLayout</TT> manager.<P>Java's <TT>GridLayout</TT> manager organizes your applet's displayinto a rectangular grid, similar to the grid used in a spreadsheet.Java then places the components you create for the applet intoeach cell of the grid, working from left to right and top to bottom.You create a <TT>GridLayout</TT> manager like this:<BLOCKQUOTE><PRE>GridLayout layout = new GridLayout(rows, cols, hor, ver);SetLayout(layout);</PRE></BLOCKQUOTE><P>The constructor's four arguments are the number of rows in thegrid, the number of columns, and the horizontal and vertical spacebetween the grid cells.<H3><A NAME="CreatingaIGridLayoutIManager">Creating a <I>GridLayout</I> Manager</A></H3><P>To test the <TT>GridLayout</TT> manager, suppose you want to placefour buttons into a 2<TT>x</TT>2 grid, with no space between thebuttons. Listing 22.3 shows how you'd create the manager and thebuttons for the applet. Figure 22.5 shows the resultant controllayout. Figure 22.6 shows the same layout manager, except createdwith horizontal and vertical spacing of 10, and Figure 22.7 showsthe layout with a single row of four cells.<P><A HREF="f22-5.gif"><B> Figure 22.5 : </B><I>This GridLayout manager is set to two rows and two columns.</I></A><P><P><A HREF="f22-6.gif"><B> Figure 22.6 : </B><I>This is the same GridLayout manager with horizontal and vertical spacing.</I></A><P><P><A HREF="f22-7.gif"><B> Figure 22.7 : </B><I>This GridLayout manager has one row and four columns.</I></A><P><HR><BLOCKQUOTE><B>Listing 22.3 LST22_3.TXT: Creating a </B><I>GridLayout</I><B>Manager.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>GridLayout layout = new GridLayout(2, 2, 0, 0);setLayout(layout);button1 = new Button("Button1");button2 = new Button("Button2");button3 = new Button("Button3");button4 = new Button("Button4");add(button1);add(button2);add(button3);add(button4);</PRE></BLOCKQUOTE><HR><H2><A NAME="TheIBorderLayoutIManager"><FONT SIZE=5 COLOR=#Ff0000>The <I>BorderLayout</I> Manager</FONT></A></H2><P>You'll probably use the <TT>GridLayout</TT> manager most of thetime, but there may be cases where you need to put together somethinga little more unusual. One layout you can try is provided by the<TT>BorderLayout</TT> manager, which enables you to position componentsusing the directions north, south, east, west, and center. Youcreate a <TT>BorderLayout</TT> manager object like this:<BLOCKQUOTE><PRE>BorderLayout layout = new BorderLayout(hor, ver);setLayout(layout);</PRE></BLOCKQUOTE><P>This constructor's two arguments are the horizontal and verticalspacing between the cells in the layout.<P>After you create the <TT>BorderLayout</TT> object, you must addthe components using a different version of the <TT>add()</TT>method:<BLOCKQUOTE><PRE>add(position, object);</PRE></BLOCKQUOTE><P>Here, <TT>position</TT> is where to place the component and mustbe the string <TT>North</TT>, <TT>South</TT>, <TT>East</TT>, <TT>West</TT>,or <TT>Center</TT>. The second argument, <TT>object</TT>, is thecomponent you want to add to the applet.<H3><A NAME="CreatingaIBorderLayoutIManager">Creating a <I>BorderLayout</I> Manager</A></H3><P>Suppose you have five buttons that you want to place in the fiveareas supported by a <TT>BorderLayout</TT> manager. First, youcreate and set the manager. Then, you create the five buttonsand add them to the applet, using the special version of <TT>add()</TT>that includes the object's position as the first argument. Listing22.4 shows how this is done. Figure 22.8 shows the resultant display,whereas Figure 22.9 shows the same applet with the <TT>BorderLayout</TT>manager with horizontal and vertical spacing.<P><A HREF="f22-9.gif"><B> Figure 22.9 : </B><I>This is the same applet with horizontal and vertical spacing.</I></A><P><P><A HREF="f22-8.gif"><B> Figure 22.8 : </B><I>This applet displays five buttons using a BorderLayout manager.</I></A><P><HR><BLOCKQUOTE><B>Listing 22.4 LST22_4.TXT: Creating a </B><I>BorderLayout</I><B>Manager.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>BorderLayout layout = new BorderLayout(0, 0);setLayout(layout);button1 = new Button("Button1");button2 = new Button("Button2");button3 = new Button("Button3");button4 = new Button("Button4");button5 = new Button("Button5");add("North", button1);add("South", button2);add("East", button3);add("West", button4);add("Center", button5);</PRE></BLOCKQUOTE><HR><H2><A NAME="TheICardLayoutIManager"><FONT SIZE=5 COLOR=#Ff0000>The <I>CardLayout</I> Manager</FONT></A></H2><P>One of the most complex layout managers is <TT>CardLayout</TT>.Using this manager, you can create a stack of layouts not unlikea stack of cards and then flip from one layout to another. Thistype of display organization is not unlike Windows 95's tabbeddialogs, usually called property sheets. To create a layout withthe <TT>CardLayout</TT> manager, you first create a parent panelto hold the "cards." Then, you create the <TT>CardLayout</TT>object and set it as the panel's layout manager. Finally, youadd each "card" to the layout by creating the componentsand adding them to the panel.<P>To create a <TT>CardLayout</TT> manager, call its constructorand then add it to the applet, like this:<BLOCKQUOTE><PRE>CardLayout cardLayout = new CardLayout(hor, ver);panel.setLayout(cardLayout);</PRE></BLOCKQUOTE><P>The constructor's two arguments are the horizontal and verticalspacing.<H3><A NAME="TheICardLayoutIManagerMethods">The <I>CardLayout</I> Manager Methods</A></H3><P>Because the <TT>CardLayout</TT> manager enables you to switchbetween a stack of layouts, you need some way to tell the managerwhat to do. For this reason, the <TT>CardLayout</TT> manager hasa number of public methods that you can call to specify whichcard is visible on the screen. Table 22.1 lists the most usefulof these methods along with their descriptions.<BR><P><CENTER><B>Table 22.1 </B><I>CardLayout</I><B> ManagerMethods.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=258><I><B>Method</B></I></TD><TD WIDTH=196><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=258>first(Container parent)</TD><TD WIDTH=196>Displays the first card.</TD></TR><TR VALIGN=TOP><TD WIDTH=258>last(Container parent)</TD><TD WIDTH=196>Displays the last card.</TD></TR><TR VALIGN=TOP><TD WIDTH=258>next(Container parent)</TD><TD WIDTH=196>Displays the next card.</TD></TR><TR VALIGN=TOP><TD WIDTH=258>previous(Container parent)</TD><TD WIDTH=196>Displays the previous card.</TD></TR><TR VALIGN=TOP><TD WIDTH=258>show(Container parent, String name)</TD><TD WIDTH=196>Displays the specified card.</TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleCreatingaICardLayoutIManager">Example: Creating a <I>CardLayout</I> Manager</A></H3><P>Putting the <TT>CardLayout</TT> manager to work is a lot easierif you always keep in mind the hierarchy of components. At thebottom of the stack is the applet's display area. On top of thisstack is the component (usually a panel) that will hold the "cards."On top of the parent component is the <TT>CardLayout</TT> manager,which you can think of as a deck of cards. The cards in this deckare the components that you add to the panel.<P>Listing 22.5 is an applet that demonstrates how all this works.The cards in this applet are the three buttons. When you run theapplet, you see a single button in the display (Figure 22.10).Click the button to switch to the next button in the stack. Whenyou get to button three and click it, you end up back at buttonone. You can cycle through the buttons as often as you like.<P><A HREF="f22-10.gif"><B> Figure 22.10 : </B><I>Clicking the button switches the manager to a new card.</I></A><P><HR><BLOCKQUOTE><B>Listing 22.5 CardApplet.java: Using a </B><I>CardLayout</I><B>Manager.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class CardApplet extends Applet{ CardLayout cardLayout; Panel panel; Button button1, button2, button3; public void init() { panel = new Panel(); add(panel); cardLayout = new CardLayout(0, 0); panel.setLayout(cardLayout);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -