📄 gridbaglayout.java
字号:
/* * @(#)GridBagLayout.java 1.38 06/10/10 * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package java.awt;import java.util.Hashtable;import java.util.Vector;class GridBagLayoutInfo implements java.io.Serializable { int width, height; /* number of cells horizontally, vertically */ int startx, starty; /* starting point for layout */ int minWidth[]; /* largest minWidth in each column */ int minHeight[]; /* largest minHeight in each row */ double weightX[]; /* largest weight in each column */ double weightY[]; /* largest weight in each row */ GridBagLayoutInfo () { minWidth = new int[GridBagLayout.MAXGRIDSIZE]; minHeight = new int[GridBagLayout.MAXGRIDSIZE]; weightX = new double[GridBagLayout.MAXGRIDSIZE]; weightY = new double[GridBagLayout.MAXGRIDSIZE]; }}/** * The <code>GridBagLayout</code> class is a flexible layout * manager that aligns components vertically and horizontally, * without requiring that the components be of the same size. * Each <code>GridBagLayout</code> object maintains a dynamic * rectangular grid of cells, with each component occupying * one or more cells, called its <em>display area</em>. * <p> * Each component managed by a grid bag layout is associated * with an instance of * <a href="java.awt.GridBagConstraints.html"><code>GridBagConstraints</code></a> * that specifies how the component is laid out within its display area. * <p> * How a <code>GridBagLayout</code> object places a set of components * depends on the <code>GridBagConstraints</code> object associated * with each component, and on the minimum size * and the preferred size of the components' containers. * <p> * To use a grid bag layout effectively, you must customize one or more * of the <code>GridBagConstraints</code> objects that are associated * with its components. You customize a <code>GridBagConstraints</code> * object by setting one or more of its instance variables: * <p> * <dl> * <dt><a href="java.awt.GridBagConstraints.html#gridx"><code>gridx</code></a>, * <a href="java.awt.GridBagConstraints.html#gridy"><code>gridy</code></a> * <dd>Specifies the cell at the upper left of the component's display area, * where the upper-left-most cell has address * <code>gridx = 0</code>, * <code>gridy = 0</code>. * Use <code>GridBagConstraints.RELATIVE</code> (the default value) * to specify that the component be just placed * just to the right of (for <code>gridx</code>) * or just below (for <code>gridy</code>) * the component that was added to the container * just before this component was added. * <dt><a href="java.awt.GridBagConstraints.html#gridwidth"><code>gridwidth</code></a>, * <a href="java.awt.GridBagConstraints.html#gridheight"><code>gridheight</code></a> * <dd>Specifies the number of cells in a row (for <code>gridwidth</code>) * or column (for <code>gridheight</code>) * in the component's display area. * The default value is 1. * Use <code>GridBagConstraints.REMAINDER</code> to specify * that the component be the last one in its row (for <code>gridwidth</code>) * or column (for <code>gridheight</code>). * Use <code>GridBagConstraints.RELATIVE</code> to specify * that the component be the next to last one * in its row (for <code>gridwidth</code>) * or column (for <code>gridheight</code>). * <dt><a href="java.awt.GridBagConstraints.html#fill"><code>fill</code></a> * <dd>Used when the component's display area * is larger than the component's requested size * to determine whether (and how) to resize the component. * Possible values are * <code>GridBagConstraints.NONE</code> (the default), * <code>GridBagConstraints.HORIZONTAL</code> * (make the component wide enough to fill its display area * horizontally, but don't change its height), * <code>GridBagConstraints.VERTICAL</code> * (make the component tall enough to fill its display area * vertically, but don't change its width), and * <code>GridBagConstraints.BOTH</code> * (make the component fill its display area entirely). * <dt><a href="java.awt.GridBagConstraints.html#ipadx"><code>ipadx</code></a>, * <a href="java.awt.GridBagConstraints.html#ipady"><code>ipady</code></a> * <dd>Specifies the component's internal padding within the layout, * how much to add to the minimum size of the component. * The width of the component will be at least its minimum width * plus <code>(ipadx * 2)</code> pixels (since the padding * applies to both sides of the component). Similarly, the height of * the component will be at least the minimum height plus * <code>(ipady * 2)</code> pixels. * <dt><a href="java.awt.GridBagConstraints.html#insets"><code>insets</code></a> * <dd>Specifies the component's external padding, the minimum * amount of space between the component and the edges of its display area. * <dt><a href="java.awt.GridBagConstraints.html#anchor"><code>anchor</code></a> * <dd>Used when the component is smaller than its display area * to determine where (within the display area) to place the component. * Valid values are * <code>GridBagConstraints.CENTER</code> (the default), * <code>GridBagConstraints.NORTH</code>, * <code>GridBagConstraints.NORTHEAST</code>, * <code>GridBagConstraints.EAST</code>, * <code>GridBagConstraints.SOUTHEAST</code>, * <code>GridBagConstraints.SOUTH</code>, * <code>GridBagConstraints.SOUTHWEST</code>, * <code>GridBagConstraints.WEST</code>, and * <code>GridBagConstraints.NORTHWEST</code>. * <dt><a href="java.awt.GridBagConstraints.html#weightx"><code>weightx</code></a>, * <a href="java.awt.GridBagConstraints.html#weighty"><code>weighty</code></a> * <dd>Used to determine how to distribute space, which is * important for specifying resizing behavior. * Unless you specify a weight for at least one component * in a row (<code>weightx</code>) and column (<code>weighty</code>), * all the components clump together in the center of their container. * This is because when the weight is zero (the default), * the <code>GridBagLayout</code> object puts any extra space * between its grid of cells and the edges of the container. * </dl> * <p> * The following figure shows ten components (all buttons) * managed by a grid bag layout: * <p> * <img src="images-awt/GridBagLayout-1.gif" * ALIGN=center HSPACE=10 VSPACE=7> * <p> * Each of the ten components has the <code>fill</code> field * of its associated <code>GridBagConstraints</code> object * set to <code>GridBagConstraints.BOTH</code>. * In addition, the components have the following non-default constraints: * <p> * <ul> * <li>Button1, Button2, Button3: <code>weightx = 1.0</code> * <li>Button4: <code>weightx = 1.0</code>, * <code>gridwidth = GridBagConstraints.REMAINDER</code> * <li>Button5: <code>gridwidth = GridBagConstraints.REMAINDER</code> * <li>Button6: <code>gridwidth = GridBagConstraints.RELATIVE</code> * <li>Button7: <code>gridwidth = GridBagConstraints.REMAINDER</code> * <li>Button8: <code>gridheight = 2</code>, * <code>weighty = 1.0</code> * <li>Button9, Button 10: * <code>gridwidth = GridBagConstraints.REMAINDER</code> * </ul> * <p> * Here is the code that implements the example shown above: * <p> * <hr><blockquote><pre> * import java.awt.*; * import java.util.*; * import java.applet.Applet; * * public class GridBagEx1 extends Applet { * * protected void makebutton(String name, * GridBagLayout gridbag, * GridBagConstraints c) { * Button button = new Button(name); * gridbag.setConstraints(button, c); * add(button); * } * * public void init() { * GridBagLayout gridbag = new GridBagLayout(); * GridBagConstraints c = new GridBagConstraints(); * * setFont(new Font("Helvetica", Font.PLAIN, 14)); * setLayout(gridbag); * * c.fill = GridBagConstraints.BOTH; * c.weightx = 1.0; * makebutton("Button1", gridbag, c); * makebutton("Button2", gridbag, c); * makebutton("Button3", gridbag, c); * * c.gridwidth = GridBagConstraints.REMAINDER; //end row * makebutton("Button4", gridbag, c); * * c.weightx = 0.0; //reset to the default * makebutton("Button5", gridbag, c); //another row * * c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row * makebutton("Button6", gridbag, c); * * c.gridwidth = GridBagConstraints.REMAINDER; //end row * makebutton("Button7", gridbag, c); * * c.gridwidth = 1; //reset to the default * c.gridheight = 2; * c.weighty = 1.0; * makebutton("Button8", gridbag, c); * * c.weighty = 0.0; //reset to the default * c.gridwidth = GridBagConstraints.REMAINDER; //end row * c.gridheight = 1; //reset to the default * makebutton("Button9", gridbag, c); * makebutton("Button10", gridbag, c); * * setSize(300, 100); * } * * public static void main(String args[]) { * Frame f = new Frame("GridBag Layout Example"); * GridBagEx1 ex1 = new GridBagEx1(); * * ex1.init(); * * f.add("Center", ex1); * f.pack(); * f.setSize(f.getPreferredSize()); * f.show(); * } * } * </pre></blockquote><hr> * <p> * @version 1.5, 16 Nov 1995 * @author Doug Stein * @see java.awt.GridBagConstraints * @since JDK1.0 */public class GridBagLayout implements LayoutManager2, java.io.Serializable { /** * The maximum number of grid positions (both horizontally and * vertically) that can be laid out by the grid bag layout. * @since JDK1.0 */ protected static final int MAXGRIDSIZE = 512; /** * The smallest grid that can be laid out by the grid bag layout. * @since JDK1.0 */ protected static final int MINSIZE = 1; protected static final int PREFERREDSIZE = 2; protected Hashtable comptable; protected GridBagConstraints defaultConstraints; private GridBagLayoutInfo layoutInfo; public int columnWidths[]; public int rowHeights[]; public double columnWeights[]; public double rowWeights[]; /** * Creates a grid bag layout manager. * @since JDK1.0 */ public GridBagLayout () { comptable = new Hashtable(); defaultConstraints = new GridBagConstraints(); } /** * Sets the constraints for the specified component in this layout. * @param comp the component to be modified. * @param constraints the constraints to be applied. * @since JDK1.0 */ public void setConstraints(Component comp, GridBagConstraints constraints) { comptable.put(comp, constraints.clone()); } /** * Gets the constraints for the specified component. A copy of * the actual <code>GridBagConstraints</code> object is returned. * @param comp the component to be queried. * @return the constraint for the specified component in this * grid bag layout; a copy of the actual constraint * object is returned. * @since JDK1.0 */ public GridBagConstraints getConstraints(Component comp) { GridBagConstraints constraints = (GridBagConstraints) comptable.get(comp); if (constraints == null) { setConstraints(comp, defaultConstraints); constraints = (GridBagConstraints) comptable.get(comp); } return (GridBagConstraints) constraints.clone(); } /** * Retrieves the constraints for the specified component. * The return value is not a copy, but is the actual * <code>GridBagConstraints</code> object used by the layout mechanism.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -