📄 gridbaglayout.java
字号:
d = layoutInfo.starty;
for (i=0; i<layoutInfo.height; i++) {
d += layoutInfo.minHeight[i];
if (d > y)
break;
}
loc.y = i;
return loc;
}
/**
* Adds the specified component with the specified name to the layout.
* @param name the name of the component.
* @param comp the component to be added.
* @since JDK1.0
*/
public void addLayoutComponent(String name, Component comp) {
}
/**
* Adds the specified component to the layout, using the specified
* constraint object.
* @param comp the component to be added.
* @param constraints an object that determines how
* the component is added to the layout.
* @since JDK1.0
*/
public void addLayoutComponent(Component comp, Object constraints) {
if (constraints instanceof GridBagConstraints) {
setConstraints(comp, (GridBagConstraints)constraints);
}else if (constraints instanceof String){
//Netscape : Just Ignore it
}else if (constraints != null) {
throw new IllegalArgumentException("cannot add to layout: constraint must be a GridBagConstraint");
}
}
/**
* Removes the specified component from this layout.
* <p>
* Most applications do not call this method directly.
* @param comp the component to be removed.
* @see java.awt.Container#remove(java.awt.Component)
* @see java.awt.Container#removeAll()
* @since JDK1.0
*/
public void removeLayoutComponent(Component comp) {
}
/**
* Determines the preferred size of the <code>target</code>
* container using this grid bag layout.
* <p>
* Most applications do not call this method directly.
* @param target the container in which to do the layout.
* @see java.awt.Container#getPreferredSize
* @since JDK1.0
*/
public Dimension preferredLayoutSize(Container parent) {
GridBagLayoutInfo info = GetLayoutInfo(parent, PREFERREDSIZE);
return GetMinSize(parent, info);
}
/**
* Determines the minimum size of the <code>target</code> container
* using this grid bag layout.
* <p>
* Most applications do not call this method directly.
* @param target the container in which to do the layout.
* @see java.awt.Container#doLayout
* @since JDK1.0
*/
public Dimension minimumLayoutSize(Container parent) {
GridBagLayoutInfo info = GetLayoutInfo(parent, MINSIZE);
return GetMinSize(parent, info);
}
/**
* Returns the maximum dimensions for this layout given the components
* in the specified target container.
* @param target the component which needs to be laid out
* @see Container
* @see #minimumLayoutSize
* @see #preferredLayoutSize
*/
public Dimension maximumLayoutSize(Container target) {
return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
}
/**
* Returns the alignment along the x axis. This specifies how
* the component would like to be aligned relative to other
* components. The value should be a number between 0 and 1
* where 0 represents alignment along the origin, 1 is aligned
* the furthest away from the origin, 0.5 is centered, etc.
*/
public float getLayoutAlignmentX(Container parent) {
return 0.5f;
}
/**
* Returns the alignment along the y axis. This specifies how
* the component would like to be aligned relative to other
* components. The value should be a number between 0 and 1
* where 0 represents alignment along the origin, 1 is aligned
* the furthest away from the origin, 0.5 is centered, etc.
*/
public float getLayoutAlignmentY(Container parent) {
return 0.5f;
}
/**
* Invalidates the layout, indicating that if the layout manager
* has cached information it should be discarded.
*/
public void invalidateLayout(Container target) {
}
/**
* Lays out the specified container using this grid bag layout.
* This method reshapes components in the specified container in
* order to satisfy the contraints of this <code>GridBagLayout</code>
* object.
* <p>
* Most applications do not call this method directly.
* @param parent the container in which to do the layout.
* @see java.awt.Container
* @see java.awt.Container#doLayout
* @since JDK1.0
*/
public void layoutContainer(Container parent) {
ArrangeGrid(parent);
}
/**
* Returns a string representation of this grid bag layout's values.
* @return a string representation of this grid bag layout.
* @since JDK1.0
*/
public String toString() {
return getClass().getName();
}
/**
* Print the layout information. Useful for debugging.
*/
/* DEBUG
*
* protected void DumpLayoutInfo(GridBagLayoutInfo s) {
* int x;
*
* System.out.println("Col\tWidth\tWeight");
* for (x=0; x<s.width; x++) {
* System.out.println(x + "\t" +
* s.minWidth[x] + "\t" +
* s.weightX[x]);
* }
* System.out.println("Row\tHeight\tWeight");
* for (x=0; x<s.height; x++) {
* System.out.println(x + "\t" +
* s.minHeight[x] + "\t" +
* s.weightY[x]);
* }
* }
*/
/**
* Print the layout constraints. Useful for debugging.
*/
/* DEBUG
*
* protected void DumpConstraints(GridBagConstraints constraints) {
* System.out.println(
* "wt " +
* constraints.weightx +
* " " +
* constraints.weighty +
* ", " +
*
* "box " +
* constraints.gridx +
* " " +
* constraints.gridy +
* " " +
* constraints.gridwidth +
* " " +
* constraints.gridheight +
* ", " +
*
* "min " +
* constraints.minWidth +
* " " +
* constraints.minHeight +
* ", " +
*
* "pad " +
* constraints.insets.bottom +
* " " +
* constraints.insets.left +
* " " +
* constraints.insets.right +
* " " +
* constraints.insets.top +
* " " +
* constraints.ipadx +
* " " +
* constraints.ipady);
* }
*/
/*
* Fill in an instance of the above structure for the current set
* of managed children. This requires three passes through the
* set of children:
*
* 1) Figure out the dimensions of the layout grid
* 2) Determine which cells the components occupy
* 3) Distribute the weights and min sizes amoung the rows/columns.
*
* This also caches the minsizes for all the children when they are
* first encountered (so subsequent loops don't need to ask again).
*/
protected GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag) {
synchronized (parent.getTreeLock()) {
GridBagLayoutInfo r = new GridBagLayoutInfo();
Component comp;
GridBagConstraints constraints;
Dimension d;
Component components[] = parent.getComponents();
int compindex, i, j, k, px, py, pixels_diff, nextSize;
int curX, curY, curWidth, curHeight, curRow, curCol;
double weight_diff, weight, start, size;
int xMax[], yMax[];
/*
* Pass #1
*
* Figure out the dimensions of the layout grid (use a value of 1 for
* zero or negative widths and heights).
*/
r.width = r.height = 0;
curRow = curCol = -1;
xMax = new int[MAXGRIDSIZE];
yMax = new int[MAXGRIDSIZE];
for (compindex = 0 ; compindex < components.length ; compindex++) {
comp = components[compindex];
if (!comp.isVisible())
continue;
constraints = lookupConstraints(comp);
curX = constraints.gridx;
curY = constraints.gridy;
curWidth = constraints.gridwidth;
if (curWidth <= 0)
curWidth = 1;
curHeight = constraints.gridheight;
if (curHeight <= 0)
curHeight = 1;
/* If x or y is negative, then use relative positioning: */
if (curX < 0 && curY < 0) {
if (curRow >= 0)
curY = curRow;
else if (curCol >= 0)
curX = curCol;
else
curY = 0;
}
if (curX < 0) {
px = 0;
for (i = curY; i < (curY + curHeight); i++)
px = Math.max(px, xMax[i]);
curX = px - curX - 1;
if(curX < 0)
curX = 0;
}
else if (curY < 0) {
py = 0;
for (i = curX; i < (curX + curWidth); i++)
py = Math.max(py, yMax[i]);
curY = py - curY - 1;
if(curY < 0)
curY = 0;
}
/* Adjust the grid width and height */
for (px = curX + curWidth; r.width < px; r.width++);
for (py = curY + curHeight; r.height < py; r.height++);
/* Adjust the xMax and yMax arrays */
for (i = curX; i < (curX + curWidth); i++) { yMax[i] = py; }
for (i = curY; i < (curY + curHeight); i++) { xMax[i] = px; }
/* Cache the current slave's size. */
if (sizeflag == PREFERREDSIZE)
d = comp.getPreferredSize();
else
d = comp.getMinimumSize();
constraints.minWidth = d.width;
constraints.minHeight = d.height;
/* Zero width and height must mean that this is the last item (or
* else something is wrong). */
if (constraints.gridheight == 0 && constraints.gridwidth == 0)
curRow = curCol = -1;
/* Zero width starts a new row */
if (constraints.gridheight == 0 && curRow < 0)
curCol = curX + curWidth;
/* Zero height starts a new column */
else if (constraints.gridwidth == 0 && curCol < 0)
curRow = curY + curHeight;
}
/*
* Apply minimum row/column dimensions
*/
if (columnWidths != null && r.width < columnWidths.length)
r.width = columnWidths.length;
if (rowHeights != null && r.height < rowHeights.length)
r.height = rowHeights.length;
/*
* Pass #2
*
* Negative values for gridX are filled in with the current x value.
* Negative values for gridY are filled in with the current y value.
* Negative or zero values for gridWidth and gridHeight end the current
* row or column, respectively.
*/
curRow = curCol = -1;
xMax = new int[MAXGRIDSIZE];
yMax = new int[MAXGRIDSIZE];
for (compindex = 0 ; compindex < components.length ; compindex++) {
comp = components[compindex];
if (!comp.isVisible())
continue;
constraints = lookupConstraints(comp);
curX = constraints.gridx;
curY = constraints.gridy;
curWidth = constraints.gridwidth;
curHeight = constraints.gridheight;
/* If x or y is negative, then use relative positioning: */
if (curX < 0 && curY < 0) {
if(curRow >= 0)
curY = curRow;
else if(curCol >= 0)
curX = curCol;
else
curY = 0;
}
if (curX < 0) {
if (curHeight <= 0) {
curHeight += r.height - curY;
if (curHeight < 1)
curHeight = 1;
}
px = 0;
for (i = curY; i < (curY + curHeight); i++)
px = Math.max(px, xMax[i]);
curX = px - curX - 1;
if(curX < 0)
curX = 0;
}
else if (curY < 0) {
if (curWidth <= 0) {
curWidth += r.width - curX;
if (curWidth < 1)
curWidth = 1;
}
py = 0;
for (i = curX; i < (curX + curWidth); i++)
py = Math.max(py, yMax[i]);
curY = py - curY - 1;
if(curY < 0)
curY = 0;
}
if (curWidth <= 0) {
curWidth += r.width - curX;
if (curWidth < 1)
curWidth = 1;
}
if (curHeight <= 0) {
curHeight += r.height - curY;
if (curHeight < 1)
curHeight = 1;
}
px = curX + curWidth;
py = curY + curHeight;
for (i = curX; i < (curX + curWidth); i++) { yMax[i] = py; }
for (i = curY; i < (curY + curHeight); i++) { xMax[i] = px; }
/* Make negative sizes start a new row/column */
if (constraints.gridheight == 0 && constraints.gridwidth == 0)
curRow = curCol = -1;
if (constraints.gridheight == 0 && curRow < 0)
curCol = curX + curWidth;
else if (constraints.gridwidth == 0 && curCol < 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -