📄 gridbaglayout.java
字号:
/*
* @(#)GridBagLayout.java 1.23 98/01/09
*
* Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*
* CopyrightVersion 1.1_beta
*
*/
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;
protected 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.
* @param comp the component to be queried
* @return the contraints for the specified component.
* @since JDK1.0
*/
protected GridBagConstraints lookupConstraints(Component comp) {
GridBagConstraints constraints = (GridBagConstraints)comptable.get(comp);
if (constraints == null) {
setConstraints(comp, defaultConstraints);
constraints = (GridBagConstraints)comptable.get(comp);
}
return constraints;
}
/**
* Determines the origin of the layout grid.
* Most applications do not call this method directly.
* @return the origin of the cell in the top-left
* corner of the layout grid.
* @since JDK1.1
*/
public Point getLayoutOrigin () {
Point origin = new Point(0,0);
if (layoutInfo != null) {
origin.x = layoutInfo.startx;
origin.y = layoutInfo.starty;
}
return origin;
}
/**
* Determines column widths and row heights for the layout grid.
* <p>
* Most applications do not call this method directly.
* @return an array of two arrays, containing the widths
* of the layout columns and
* the heights of the layout rows.
* @since JDK1.1
*/
public int [][] getLayoutDimensions () {
if (layoutInfo == null)
return new int[2][0];
int dim[][] = new int [2][];
dim[0] = new int[layoutInfo.width];
dim[1] = new int[layoutInfo.height];
System.arraycopy(layoutInfo.minWidth, 0, dim[0], 0, layoutInfo.width);
System.arraycopy(layoutInfo.minHeight, 0, dim[1], 0, layoutInfo.height);
return dim;
}
/**
* Determines the weights of the layout grid's columns and rows.
* Weights are used to calculate how much a given column or row
* stretches beyond its preferred size, if the layout has extra
* room to fill.
* <p>
* Most applications do not call this method directly.
* @return an array of two arrays, representing the
* horizontal weights of the layout columns
* and the vertical weights of the layout rows.
* @since JDK1.1
*/
public double [][] getLayoutWeights () {
if (layoutInfo == null)
return new double[2][0];
double weights[][] = new double [2][];
weights[0] = new double[layoutInfo.width];
weights[1] = new double[layoutInfo.height];
System.arraycopy(layoutInfo.weightX, 0, weights[0], 0, layoutInfo.width);
System.arraycopy(layoutInfo.weightY, 0, weights[1], 0, layoutInfo.height);
return weights;
}
/**
* Determines which cell in the layout grid contains the point
* specified by <code>(x, y)</code>. Each cell is identified
* by its column index (ranging from 0 to the number of columns
* minus 1) and its row index (ranging from 0 to the number of
* rows minus 1).
* <p>
* If the <code>(x, y)</code> point lies
* outside the grid, the following rules are used.
* The column index is returned as zero if <code>x</code> lies to the
* left of the layout, and as the number of columns if <code>x</code> lies
* to the right of the layout. The row index is returned as zero
* if <code>y</code> lies above the layout,
* and as the number of rows if <code>y</code> lies
* below the layout.
* @param x the <i>x</i> coordinate of a point.
* @param y the <i>y</i> coordinate of a point.
* @return an ordered pair of indexes that indicate which cell
* in the layout grid contains the point
* (<i>x</i>, <i>y</i>).
* @since JDK1.1
*/
public Point location(int x, int y) {
Point loc = new Point(0,0);
int i, d;
if (layoutInfo == null)
return loc;
d = layoutInfo.startx;
for (i=0; i<layoutInfo.width; i++) {
d += layoutInfo.minWidth[i];
if (d > x)
break;
}
loc.x = i;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -