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

📄 ch22.htm

📁 Java_by_Example,初级经典例子哦,珍藏版本
💻 HTM
📖 第 1 页 / 共 4 页
字号:
        button1 = new Button(&quot;Button1&quot;);        button2 = new Button(&quot;Button2&quot;);        button3 = new Button(&quot;Button3&quot;);        panel.add(&quot;Button1&quot;, button1);        panel.add(&quot;Button2&quot;, button2);        panel.add(&quot;Button3&quot;, button3);    }    public boolean action(Event evt, Object arg)    {        cardLayout.next(panel);        return true;    }}</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>CardApplet</TT> class from Java's <TT>Applet</TT>class.<BR>    Declare the layout, panel, and button objects.<BR>    Override the <TT>init()</TT> method.<BR>        Create and add the parent panel.<BR>        Create and set the layout.<BR>        Create the buttons (which act as the cards).<BR>        Add the buttons to the panel.<BR>    Override the <TT>action()</TT> method.<BR>        Switch to the next card (button).<BR>        Tell Java that the event was handled okay.<BR></BLOCKQUOTE><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>The stack of cards that are arranged by a <TT>CardLayout</TT> manager can be any type of component. For example, you can create several different panels, each with their own controls, and switch between the panels. This enables you to switch between whole sets of controls, just like Windows 95's property sheets.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="TheIGridBagLayoutIManager"><FONT SIZE=5 COLOR=#Ff0000>The <I>GridBagLayout</I> Manager</FONT></A></H2><P>The most complex of the layout managers is <TT>GridBagLayout</TT>,which pretty much lets you organize objects any way you like.However, the price for this power is meticulous planning and alot of experimentation. At the time of this writing, the documentationfor the <TT>GridBagLayout</TT> manager was sketchy and incomplete.I did the best I could to figure out exactly how this layout managerworked, but there's no question that to get the best out of <TT>GridBagLayout</TT>,you're going to have to spend some time experimenting with differentlayouts.<P>To create a layout using <TT>GridBagLayout</TT>, you must followthese steps:<OL><LI>Create a <TT>GridBagLayout</TT> object.<LI>Set the layout manager.<LI>Create a <TT>GridBagConstraints</TT> object.<LI>Initialize and set the constraints for the object about tobe added to the layout.<LI>Add the object to the layout.<LI>Repeat steps 4 and 5 for each object you're adding to thelayout.</OL><P>Yep, there's much to be done to use a <TT>GridBagLayout</TT> manager.In the sections that follow, you'll learn how to perform eachof the required steps.<H3><A NAME="CreatingandSettingtheIGridBagLayoutIManager">Creating and Setting the <I>GridBagLayout</I> Manager</A></H3><P>To create a <TT>GridBagLayout</TT> manager, call the class's constructor,like this:<BLOCKQUOTE><PRE>GridBagLayout layout = new GridBagLayout();</PRE></BLOCKQUOTE><P>The constructor requires no arguments. When you've created the<TT>GridBagLayout()</TT> object, set the manager by calling <TT>setLayout()</TT>:<BLOCKQUOTE><PRE>setLayout(layout);</PRE></BLOCKQUOTE><P>This method's single argument is a reference to the layout object.<H3><A NAME="CreatingandSettingaIGridBagConstraintsIObject">Creating and Setting a <I>GridBagConstraints</I> Object</A></H3><P>Because the position of each component in a layout controlledby a <TT>GridBagLayout</TT> object is determined by the currentlyset <TT>GridBagConstraints</TT> object, you must create the <TT>GridBagConstraints</TT>object before you can start building your layout. To do this,call the class's constructor:<BLOCKQUOTE><PRE>GridBagConstraints constraints = new GridBagConstraints();</PRE></BLOCKQUOTE><P>Like the <TT>GridBagLayout</TT> class, the <TT>GridBagConstraints</TT>constructor requires no arguments. However, although the class'sfields start off initialized to default values, you'll almostalways change some of those values before adding components tothe layout. You perform this task with lines something like this:<BLOCKQUOTE><PRE>constraints.fill = GridBagConstraints.BOTH;</PRE></BLOCKQUOTE><P>This line sets the <TT>GridBagConstraints</TT> object's <TT>fill</TT>field to a constant defined in the class. Table 22.2 shows the<TT>fields</TT> of the <TT>GridBagConstraints</TT> class and whatthey mean.<BR><P><CENTER><B>Table 22.2&nbsp;&nbsp;Fields of the </B><I>GridBagConstraints</I><B>Class.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=118><CENTER><I><B>Field</B></I></CENTER></TD><TD WIDTH=472><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=118><CENTER><TT>anchor</TT></CENTER></TD><TD WIDTH=472>Where within a component's area the component should be placed. Predefined values are <TT>GridBagConstraints.NORTH</TT>, <BR><TT>GridBagConstraints.NORTHEAST</TT>, <BR><TT>GridBagConstraints.EAST</TT>,<BR><TT>GridBagConstraints.SOUTHEAST</TT>,<BR><TT>GridBagConstraints.SOUTH</TT>,<BR><TT>GridBagConstraints.SOUTHWEST</TT>,<BR><TT>GridBagConstraints.WEST</TT>,<BR><TT>GridBagConstraints.NORTHWEST</TT>, and<BR><TT>GridBagConstraints.CENTER</TT>.</TD></TR><TR VALIGN=TOP><TD WIDTH=118><CENTER><TT>fill</TT></CENTER></TD><TD WIDTH=472>Determines how to size a component when the display area is larger than the component. Predefined values you can use are <TT>GridBagConstraint.NONE</TT>,<BR><TT>GridBagConstraint.HORIZONTAL</TT>,<BR><TT>GridBagConstraint.VERTICAL</TT>, and<BR><TT>GridBagConstraint.BOTH</TT>.</TD></TR><TR VALIGN=TOP><TD WIDTH=118><CENTER><TT>gridheight</TT></CENTER></TD><TD WIDTH=472>The number of cells in each column of a component's display area.</TD></TR><TR VALIGN=TOP><TD WIDTH=118><CENTER><TT>gridwidth</TT></CENTER></TD><TD WIDTH=472>The number of cells in each row of a component's display area.</TD></TR><TR VALIGN=TOP><TD WIDTH=118><CENTER><TT>gridx</TT></CENTER></TD><TD WIDTH=472>The X coordinate of the cell at the upper left of a component's display area.</TD></TR><TR VALIGN=TOP><TD WIDTH=118><CENTER><TT>gridy</TT></CENTER></TD><TD WIDTH=472>The Y coordinate of the cell at the upper left of the component's display area.</TD></TR><TR VALIGN=TOP><TD WIDTH=118><CENTER><TT>insets</TT></CENTER></TD><TD WIDTH=472>The minimum amount of space between a component and the edges of its display area.</TD></TR><TR VALIGN=TOP><TD WIDTH=118><CENTER><TT>ipadx</TT></CENTER></TD><TD WIDTH=472>The amount of horizontal space around a component.</TD></TR><TR VALIGN=TOP><TD WIDTH=118><CENTER><TT>ipady</TT></CENTER></TD><TD WIDTH=472>The amount of vertical space around a component.</TD></TR><TR VALIGN=TOP><TD WIDTH=118><CENTER><TT>weightx</TT></CENTER></TD><TD WIDTH=472>Determines whether components stretch horizontally to fill the applet's display area.</TD></TR><TR VALIGN=TOP><TD WIDTH=118><CENTER><TT>weighty</TT></CENTER></TD><TD WIDTH=472>Determines whether components stretch vertically to fill the applet's display area.</TD></TR></TABLE></CENTER><P><P>Once you have the <TT>GridBagConstraints</TT> object created andinitialized, you must set the constraints by calling the layoutobject's <TT>setConstraints()</TT> method:<BLOCKQUOTE><PRE>layout.setConstraints(component, constraints);</PRE></BLOCKQUOTE><P>This method's two arguments are a reference to the component whoseconstraints you're setting and a reference to the constraintsobject. You need to call <TT>setConstraints()</TT> for each componentyou add to the layout. After setting the constraints for the component,you add the component to the layout in the normal way, by callingthe <TT>add()</TT> method.<H3><A NAME="ExampleUsingaIGridBagLayoutIManagerinanApplet">Example: Using a <I>GridBagLayout</I> Manager in an Applet</A></H3><P>As I said before, the only way to really understand how the <TT>GridBagLayout</TT>manager works is to experiment with it on your own. This bookjust doesn't have the room to cover every detail of using thiscomplex manager. Still, I won't send you off without at leastthe basics. So, Listing 22.6 is an applet, called GridBagApplet,that demonstrates how to create and use a <TT>GridBagLayout</TT>manager. Figure 22.11 shows what the applet looks like when it'srun under Appletviewer.<P><A HREF="f22-11.gif"><B> Figure 22.11 : </B><I>GridBagLayout manager enables you to create unusual layouts.</I></A><P><HR><BLOCKQUOTE><B>Listing 22.6&nbsp;&nbsp;GridBagApplet.java: A </B><I>GridBagLayout</I><B>Applet.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class GridBagApplet extends Applet{    public void init()    {        GridBagLayout layout = new GridBagLayout();        setLayout(layout);        GridBagConstraints constraints = new GridBagConstraints();        Button button1 = new Button(&quot;Button1&quot;);        Button button2 = new Button(&quot;Button2&quot;);        Button button3 = new Button(&quot;Button3&quot;);        Button button4 = new Button(&quot;Button4&quot;);           Button button5 = new Button(&quot;Button5&quot;);        Button button6 = new Button(&quot;Button6&quot;);        Button button7 = new Button(&quot;Button7&quot;);        Button button8 = new Button(&quot;Button8&quot;);        Button button9 = new Button(&quot;Button9&quot;);           constraints.fill = GridBagConstraints.BOTH;        layout.setConstraints(button1, constraints);        add(button1);        constraints.gridwidth = GridBagConstraints.RELATIVE;        layout.setConstraints(button2, constraints);        add(button2);        constraints.gridwidth = GridBagConstraints.REMAINDER;        layout.setConstraints(button3, constraints);        add(button3);        constraints.gridwidth = GridBagConstraints.REMAINDER;        layout.setConstraints(button4, constraints);        add(button4);            constraints.gridwidth = GridBagConstraints.RELATIVE;        layout.setConstraints(button5, constraints);        add(button5);            constraints.gridwidth = GridBagConstraints.REMAINDER;        layout.setConstraints(button6, constraints);        add(button6);            constraints.gridwidth = 1;        constraints.gridheight = 2;        layout.setConstraints(button7, constraints);        add(button7);        constraints.gridwidth = GridBagConstraints.REMAINDER;        constraints.gridheight = 1;        layout.setConstraints(button8, constraints);        add(button8);        layout.setConstraints(button9, constraints);        add(button9);        resize(300, 200);    }}</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>GridBagApplet</TT> class from Java's <TT>Applet</TT>class.<BR>    Override the <TT>init()</TT> method.<BR>        Create and set the layout.<BR>        Create the constraints object.<BR>        Create nine buttons.<BR>        Initialize the fill for both vertical and horizontal.<BR>        Set the constraints for the buttons and add the buttons.<BR>        Set the applet's size.</BLOCKQUOTE><H3><A NAME="UnderstandingtheIGridBagAppletIApplet">Understanding the <I>GridBagApplet</I> Applet</A></H3><P>Although <TT>GridBagApplet</TT> contains only the <TT>init()</TT>method, there's a lot going on in the program. In this section,you'll see, line by line, exactly how the applet works. The firsttwo lines in the <TT>init()</TT> method look like this:<BLOCKQUOTE><PRE>GridBagLayout layout = new GridBagLayout();setLayout(layout);</PRE></BLOCKQUOTE>

⌨️ 快捷键说明

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