📄 tablelayout.java
字号:
// contained within this row ListIterator iterator = list.listIterator(0); while (iterator.hasNext()) { Entry entry = (Entry) iterator.next(); if ((entry.row1 == counter) && (entry.row2 == counter)) { Dimension p = (rowSpec[counter] == PREFERRED) ? entry.component.getPreferredSize() : entry.component.getMinimumSize(); int height = (p == null) ? 0 : p.height; if (maxHeight < height) maxHeight = height; } } // Assign preferred height rowSize[counter] = maxHeight; // Reduce available height availableHeight -= maxHeight; } // Remember how much space is available for relatively sized cells int relativeWidth = availableWidth; int relativeHeight = availableHeight; // Make sure relativeWidth and relativeHeight are non-negative if (relativeWidth < 0) relativeWidth = 0; if (relativeHeight < 0) relativeHeight = 0; // Assign relative widths for (counter = 0; counter < numColumn; counter++) // Is the current column an relative size if ((columnSpec[counter] > 0.0) && (columnSpec[counter] < 1.0)) { // Assign relative width columnSize[counter] = (int) (columnSpec[counter] * relativeWidth + 0.5); // Reduce available width availableWidth -= columnSize[counter]; } // Assign relative widths for (counter = 0; counter < numRow; counter++) // Is the current column an relative size if ((rowSpec[counter] > 0.0) && (rowSpec[counter] < 1.0)) { // Assign relative width rowSize[counter] = (int) (rowSpec[counter] * relativeHeight + 0.5); // Reduce available width availableHeight -= rowSize[counter]; } // Make sure availableWidth and availableHeight are non-negative if (availableWidth < 0) availableWidth = 0; if (availableHeight < 0) availableHeight = 0; // Count the number of "fill" cells int numFillWidth = 0; int numFillHeight = 0; for (counter = 0; counter < numColumn; counter++) if (columnSpec[counter] == FILL) numFillWidth++; for (counter = 0; counter < numRow; counter++) if (rowSpec[counter] == FILL) numFillHeight++; // If numFillWidth (numFillHeight) is zero, the cooresponding if statements // will always evaluate to false and the division will not occur. // If there are more than one "fill" cell, slack may occur due to rounding // errors int slackWidth = availableWidth; int slackHeight = availableHeight; // Assign "fill" cells equal amounts of the remaining space for (counter = 0; counter < numColumn; counter++) if (columnSpec[counter] == FILL) { columnSize[counter] = availableWidth / numFillWidth; slackWidth -= columnSize[counter]; } for (counter = 0; counter < numRow; counter++) if (rowSpec[counter] == FILL) { rowSize[counter] = availableHeight / numFillHeight; slackHeight -= rowSize[counter]; } // Add slack to the last "fill" cell for (counter = numColumn - 1; counter >= 0; counter--) { if (columnSpec[counter] == FILL) { columnSize[counter] += slackWidth; break; } } for (counter = numRow - 1; counter >= 0; counter--) { if (rowSpec[counter] == FILL) { rowSize[counter] += slackHeight; break; } } // Calculate offsets of each column (done for effeciency) columnOffset = new int[numColumn + 1]; columnOffset[0] = inset.left; for (counter = 0; counter < numColumn; counter++) columnOffset[counter + 1] = columnOffset[counter] + columnSize[counter]; // Calculate offsets of each row (done for effeciency) rowOffset = new int[numRow + 1]; rowOffset[0] = inset.top; for (counter = 0; counter < numRow; counter++) rowOffset[counter + 1] = rowOffset[counter] + rowSize[counter]; // Indicate that the size of the cells are known for the container's // current size dirty = false; oldWidth = totalWidth; oldHeight = totalHeight; } //****************************************************************************** //** java.awt.event.LayoutManager methods *** //****************************************************************************** /** * To lay out the specified container using this layout. This method reshapes * the components in the specified target container in order to satisfy the * constraints of all components. * * <p>User code should not have to call this method directly.</p> * * @param container container being served by this layout manager */ public void layoutContainer (Container container) { int x, y; // Coordinates of the currnet component in pixels int w, h; // Width and height of the current component in pixels // Calculate sizes if container has changed size or components were added Dimension d = container.getSize(); if (dirty || (d.width != oldWidth) || (d.height != oldHeight)) calculateSize (container); // Get components Component component[] = container.getComponents(); // Layout components for (int counter = 0; counter < component.length; counter++) { try { // Get the entry entry for the next component ListIterator iterator = list.listIterator(0); Entry entry = null; while (iterator.hasNext()) { entry = (Entry) iterator.next(); if (entry.component == component[counter]) break; else entry = null; } // Skip any components that have not been place in a specific cell if (entry == null) break; // Does the entry occupy a single cell if (entry.singleCell) { // The following block of code has been optimized so that the // preferred size of the component is only obtained if it is // needed. There are components in which the getPreferredSize // method is extremely expensive, such as data driven controls // with a large amount of data. // Get the preferred size of the component int preferredWidth = 0; int preferredHeight = 0; if ((entry.hAlign != FULL) || (entry.vAlign != FULL)) { Dimension preferredSize = component[counter].getPreferredSize(); preferredWidth = preferredSize.width; preferredHeight = preferredSize.height; } // Determine cell width and height int cellWidth = columnSize[entry.col1]; int cellHeight = rowSize[entry.row1]; // Determine the width of the component if ((entry.hAlign == FULL) || (cellWidth < preferredWidth)) // Use the width of the cell w = cellWidth; else // Use the prefered width of the component w = preferredWidth; // Determine left and right boarders switch (entry.hAlign) { case LEFT : // Align left side along left edge of cell x = columnOffset[entry.col1]; break; case RIGHT : // Align right side along right edge of cell x = columnOffset[entry.col1 + 1] - w; break; case CENTER : // Center justify component x = columnOffset[entry.col1] + ((cellWidth - w) >> 1); break; case FULL : // Align left side along left edge of cell x = columnOffset[entry.col1]; break; default : // This is a never should happen case, but just in case x = 0; } // Determine the height of the component if ((entry.vAlign == FULL) || (cellHeight < preferredHeight)) // Use the height of the cell h = cellHeight; else // Use the prefered height of the component h = preferredHeight; // Determine top and bottom boarders switch (entry.vAlign) { case TOP : // Align top side along top edge of cell y = rowOffset[entry.row1]; break; case BOTTOM : // Align right side along right edge of cell y = rowOffset[entry.row1 + 1] - h; break; case CENTER : // Center justify component y = rowOffset[entry.row1] + ((cellHeight - h) >> 1); break; case FULL : // Align right side along right edge of cell y = rowOffset[entry.row1]; break; default : // This is a never should happen case, but just in case y = 0; } } else { // Align left side with left boarder of first column x = columnOffset[entry.col1]; // Align top side along top edge of first row y = rowOffset[entry.row1]; // Align right side with right boarder of second column w = columnOffset[entry.col2 + 1] - columnOffset[entry.col1]; // Align bottom side with bottom boarder of second row h = rowOffset[entry.row2 + 1] - rowOffset[entry.row1]; } // Move and resize component component[counter].setBounds (x, y, w, h); } catch (Exception error) { // If any error occurs, skip this component continue; } } } /** * Determines the preferred size of the container argument using this layout. * The preferred size is the smallest size that, if used for the container's * size, will ensure that all components are at least as large as their * preferred size. This method cannot guarantee that all components will be * their preferred size. For example, if component A and component B are each * allocate half of the container's width and component A wants to be 10 pixels * wide while component B wants to be 100 pixels wide, they cannot both be * accommodated. Since in general components rather be larger than their * preferred size instead of smaller, component B's request will be fulfilled. * The preferred size of the container would be 200 pixels. * * @param container container being served by this layout manager * * @return a dimension indicating the container's preferred size */ public Dimension preferredLayoutSize (Container container) { Dimension size; // Preferred size of current component int scaledWidth = 0; // Preferred width of scalled components int scaledHeight = 0; // Preferred height of scalled components int temp; // Temporary variable used to compare sizes int counter; // Counting variable // Determine percentage of space allocated to fill components. This is // one minus the sum of all scalable components. double fillWidthRatio = 1.0; double fillHeightRatio = 1.0; int numFillWidth = 0; int numFillHeight = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -