📄 gridbaglayout.java
字号:
/* Assign the remainder to the bottom cell */ r.weightY[py - 1] += weight_diff; } /* * Calculate the minHeight array values. * First, figure out how tall the current slave needs to be. * Then, see if it will fit within the current minHeight values. * If it will not fit, add the difference according to the * weightY array. */ pixels_diff = constraints.minHeight + constraints.ipady + constraints.insets.top + constraints.insets.bottom; for (k = constraints.tempY; k < py; k++) pixels_diff -= r.minHeight[k]; if (pixels_diff > 0) { weight = 0.0; for (k = constraints.tempY; k < py; k++) weight += r.weightY[k]; for (k = constraints.tempY; weight > 0.0 && k < py; k++) { double wt = r.weightY[k]; int dy = (int) ((wt * ((double) pixels_diff)) / weight); r.minHeight[k] += dy; pixels_diff -= dy; weight -= wt; } /* Any leftovers go into the bottom cell */ r.minHeight[py - 1] += pixels_diff; } } else if (constraints.tempHeight > i && constraints.tempHeight < nextSize) nextSize = constraints.tempHeight; } } return r; } } /** * Adjusts the x, y, width, and height fields to the correct * values depending on the constraint geometry and pads. * @param constraints the constraints to be applied * @param r the <code>Rectangle</code> to be adjusted * @since 1.4 */ protected void adjustForGravity(GridBagConstraints constraints, Rectangle r) { AdjustForGravity(constraints, r); } /* * Adjusts the x, y, width, and height fields to the correct * values depending on the constraint geometry and pads. */ protected void AdjustForGravity(GridBagConstraints constraints, Rectangle r) { int diffx, diffy; r.x += constraints.insets.left; r.width -= (constraints.insets.left + constraints.insets.right); r.y += constraints.insets.top; r.height -= (constraints.insets.top + constraints.insets.bottom); diffx = 0; if ((constraints.fill != GridBagConstraints.HORIZONTAL && constraints.fill != GridBagConstraints.BOTH) && (r.width > (constraints.minWidth + constraints.ipadx))) { diffx = r.width - (constraints.minWidth + constraints.ipadx); r.width = constraints.minWidth + constraints.ipadx; } diffy = 0; if ((constraints.fill != GridBagConstraints.VERTICAL && constraints.fill != GridBagConstraints.BOTH) && (r.height > (constraints.minHeight + constraints.ipady))) { diffy = r.height - (constraints.minHeight + constraints.ipady); r.height = constraints.minHeight + constraints.ipady; } switch (constraints.anchor) { case GridBagConstraints.CENTER: r.x += diffx / 2; r.y += diffy / 2; break; case GridBagConstraints.NORTH: r.x += diffx / 2; break; case GridBagConstraints.NORTHEAST: r.x += diffx; break; case GridBagConstraints.EAST: r.x += diffx; r.y += diffy / 2; break; case GridBagConstraints.SOUTHEAST: r.x += diffx; r.y += diffy; break; case GridBagConstraints.SOUTH: r.x += diffx / 2; r.y += diffy; break; case GridBagConstraints.SOUTHWEST: r.y += diffy; break; case GridBagConstraints.WEST: r.y += diffy / 2; break; case GridBagConstraints.NORTHWEST: break; default: throw new IllegalArgumentException("illegal anchor value"); } } /* * Figure out the minimum size of the * master based on the information from GetLayoutInfo() */ private Dimension GetMinSize(Container parent, GridBagLayoutInfo info) { Dimension d = new Dimension(); int i, t; Insets insets = parent.getInsets(); t = 0; for (i = 0; i < info.width; i++) t += info.minWidth[i]; d.width = t + insets.left + insets.right; t = 0; for (i = 0; i < info.height; i++) t += info.minHeight[i]; d.height = t + insets.top + insets.bottom; return d; } /** * Lays out the grid. * @param parent the layout container * @since 1.4 */ protected void arrangeGrid(Container parent) { ArrangeGrid(parent); } /* * Lay out the grid */ protected void ArrangeGrid(Container parent) { Component comp; int compindex; GridBagConstraints constraints; Insets insets = parent.getInsets(); Component components[] = parent.getComponents(); Dimension d; Rectangle r = new Rectangle(); int i, diffw, diffh; double weight; GridBagLayoutInfo info; /* * If the parent has no slaves anymore, then don't do anything * at all: just leave the parent's size as-is. */ if (components.length == 0 && (columnWidths == null || columnWidths.length == 0) && (rowHeights == null || rowHeights.length == 0)) { return; } /* * Pass #1: scan all the slaves to figure out the total amount * of space needed. */ info = GetLayoutInfo(parent, PREFERREDSIZE); d = GetMinSize(parent, info); if (parent.width < d.width || parent.height < d.height) { info = GetLayoutInfo(parent, MINSIZE); d = GetMinSize(parent, info); } layoutInfo = info; r.width = d.width; r.height = d.height; /* * DEBUG * * DumpLayoutInfo(info); * for (compindex = 0 ; compindex < components.length ; compindex++) { * comp = components[compindex]; * if (!comp.isVisible()) * continue; * constraints = lookupConstraints(comp); * DumpConstraints(constraints); * } * System.out.println("minSize " + r.width + " " + r.height); */ /* * If the current dimensions of the window don't match the desired * dimensions, then adjust the minWidth and minHeight arrays * according to the weights. */ diffw = parent.width - r.width; if (diffw != 0) { weight = 0.0; for (i = 0; i < info.width; i++) weight += info.weightX[i]; if (weight > 0.0) { for (i = 0; i < info.width; i++) { int dx = (int) ((((double) diffw) * info.weightX[i]) / weight); info.minWidth[i] += dx; r.width += dx; if (info.minWidth[i] < 0) { r.width -= info.minWidth[i]; info.minWidth[i] = 0; } } } diffw = parent.width - r.width; } else { diffw = 0; } diffh = parent.height - r.height; if (diffh != 0) { weight = 0.0; for (i = 0; i < info.height; i++) weight += info.weightY[i]; if (weight > 0.0) { for (i = 0; i < info.height; i++) { int dy = (int) ((((double) diffh) * info.weightY[i]) / weight); info.minHeight[i] += dy; r.height += dy; if (info.minHeight[i] < 0) { r.height -= info.minHeight[i]; info.minHeight[i] = 0; } } } diffh = parent.height - r.height; } else { diffh = 0; } /* * DEBUG * * System.out.println("Re-adjusted:"); * DumpLayoutInfo(info); */ /* * Now do the actual layout of the slaves using the layout information * that has been collected. */ info.startx = diffw / 2 + insets.left; info.starty = diffh / 2 + insets.top; for (compindex = 0; compindex < components.length; compindex++) { comp = components[compindex]; if (!comp.isVisible()) continue; constraints = lookupConstraints(comp); r.x = info.startx; for (i = 0; i < constraints.tempX; i++) r.x += info.minWidth[i]; r.y = info.starty; for (i = 0; i < constraints.tempY; i++) r.y += info.minHeight[i]; r.width = 0; for (i = constraints.tempX; i < (constraints.tempX + constraints.tempWidth); i++) { r.width += info.minWidth[i]; } r.height = 0; for (i = constraints.tempY; i < (constraints.tempY + constraints.tempHeight); i++) { r.height += info.minHeight[i]; } AdjustForGravity(constraints, r); /* * If the window is too small to be interesting then * unmap it. Otherwise configure it and then make sure * it's mapped. */ if ((r.width <= 0) || (r.height <= 0)) { comp.setBounds(0, 0, 0, 0); } else { if (comp.x != r.x || comp.y != r.y || comp.width != r.width || comp.height != r.height) { comp.setBounds(r.x, r.y, r.width, r.height); } } } } // Added for serial backwards compatability (4348425) static final long serialVersionUID = 8838754796412211005L;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -