📄 gridbaglayout.java
字号:
curRow = curY + curHeight;
/* Assign the new values to the gridbag slave */
constraints.tempX = curX;
constraints.tempY = curY;
constraints.tempWidth = curWidth;
constraints.tempHeight = curHeight;
}
/*
* Apply minimum row/column dimensions and weights
*/
if (columnWidths != null)
System.arraycopy(columnWidths, 0, r.minWidth, 0, columnWidths.length);
if (rowHeights != null)
System.arraycopy(rowHeights, 0, r.minHeight, 0, rowHeights.length);
if (columnWeights != null)
System.arraycopy(columnWeights, 0, r.weightX, 0, columnWeights.length);
if (rowWeights != null)
System.arraycopy(rowWeights, 0, r.weightY, 0, rowWeights.length);
/*
* Pass #3
*
* Distribute the minimun widths and weights:
*/
nextSize = Integer.MAX_VALUE;
for (i = 1;
i != Integer.MAX_VALUE;
i = nextSize, nextSize = Integer.MAX_VALUE) {
for (compindex = 0 ; compindex < components.length ; compindex++) {
comp = components[compindex];
if (!comp.isVisible())
continue;
constraints = lookupConstraints(comp);
if (constraints.tempWidth == i) {
px = constraints.tempX + constraints.tempWidth; /* right column */
/*
* Figure out if we should use this slave\'s weight. If the weight
* is less than the total weight spanned by the width of the cell,
* then discard the weight. Otherwise split the difference
* according to the existing weights.
*/
weight_diff = constraints.weightx;
for (k = constraints.tempX; k < px; k++)
weight_diff -= r.weightX[k];
if (weight_diff > 0.0) {
weight = 0.0;
for (k = constraints.tempX; k < px; k++)
weight += r.weightX[k];
for (k = constraints.tempX; weight > 0.0 && k < px; k++) {
double wt = r.weightX[k];
double dx = (wt * weight_diff) / weight;
r.weightX[k] += dx;
weight_diff -= dx;
weight -= wt;
}
/* Assign the remainder to the rightmost cell */
r.weightX[px-1] += weight_diff;
}
/*
* Calculate the minWidth array values.
* First, figure out how wide the current slave needs to be.
* Then, see if it will fit within the current minWidth values.
* If it will not fit, add the difference according to the
* weightX array.
*/
pixels_diff =
constraints.minWidth + constraints.ipadx +
constraints.insets.left + constraints.insets.right;
for (k = constraints.tempX; k < px; k++)
pixels_diff -= r.minWidth[k];
if (pixels_diff > 0) {
weight = 0.0;
for (k = constraints.tempX; k < px; k++)
weight += r.weightX[k];
for (k = constraints.tempX; weight > 0.0 && k < px; k++) {
double wt = r.weightX[k];
int dx = (int)((wt * ((double)pixels_diff)) / weight);
r.minWidth[k] += dx;
pixels_diff -= dx;
weight -= wt;
}
/* Any leftovers go into the rightmost cell */
r.minWidth[px-1] += pixels_diff;
}
}
else if (constraints.tempWidth > i && constraints.tempWidth < nextSize)
nextSize = constraints.tempWidth;
if (constraints.tempHeight == i) {
py = constraints.tempY + constraints.tempHeight; /* bottom row */
/*
* Figure out if we should use this slave\'s weight. If the weight
* is less than the total weight spanned by the height of the cell,
* then discard the weight. Otherwise split it the difference
* according to the existing weights.
*/
weight_diff = constraints.weighty;
for (k = constraints.tempY; k < py; k++)
weight_diff -= r.weightY[k];
if (weight_diff > 0.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];
double dy = (wt * weight_diff) / weight;
r.weightY[k] += dy;
weight_diff -= dy;
weight -= wt;
}
/* 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.
*/
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()
*/
protected 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;
}
/*
* 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);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -