📄 tablelayout.java
字号:
// Determine total percentage of scalable space that the component
// occupies by adding the relative columns and the fill columns
double relativeHeight = 0.0;
for (counter = entry.row1; counter <= entry.row2; counter++)
{
// Row is scaled
if ((rowSpec[counter] > 0.0) && (rowSpec[counter] < 1.0))
// Add scaled size to relativeHeight
relativeHeight += rowSpec[counter];
// Row is fill
else if ((rowSpec[counter] == FILL) && (fillHeightRatio != 0.0))
// Add fill size to relativeHeight
relativeHeight += fillHeightRatio;
}
// Determine the total scaled width as estimated by this component
if (relativeHeight == 0)
temp = 0;
else
temp = (int) (scalableHeight / relativeHeight + 0.5);
// If the container needs to be bigger, make it so
if (scaledHeight < temp)
scaledHeight = temp;
}
// totalWidth is the scaledWidth plus the sum of all absolute widths and all
// preferred widths
int totalWidth = scaledWidth;
for (counter = 0; counter < columnSpec.length; counter++)
// Is the current column an absolute size
if (columnSpec[counter] >= 1.0)
totalWidth += (int) (columnSpec[counter] + 0.5);
// Is the current column a preferred/minimum size
else if ((columnSpec[counter] == PREFERRED) ||
(columnSpec[counter] == MINIMUM))
{
// Add preferred/minimum width
totalWidth += columnPrefMin[counter];
}
// totalHeight is the scaledHeight plus the sum of all absolute heights and
// all preferred widths
int totalHeight = scaledHeight;
for (counter = 0; counter < rowSpec.length; counter++)
// Is the current row an absolute size
if (rowSpec[counter] >= 1.0)
totalHeight += (int) (rowSpec[counter] + 0.5);
// Is the current row a preferred size
else if ((rowSpec[counter] == PREFERRED) ||
(rowSpec[counter] == MINIMUM))
{
// Add preferred/minimum width
totalHeight += rowPrefMin[counter];
}
// Compensate for container's insets
Insets inset = container.getInsets();
totalWidth += inset.left + inset.right;
totalHeight += inset.top + inset.bottom;
return new Dimension(totalWidth, totalHeight);
}
/**
* Determines the minimum size of the container argument using this layout.
* The minimum 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
* minimum size. This method cannot guarantee that all components will be
* their minimum 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
* minimum size instead of smaller, component B's request will be fulfilled.
* The minimum size of the container would be 200 pixels.
*
* @param container container being served by this layout manager
*
* @return a dimension indicating the container's minimum size
*/
public Dimension minimumLayoutSize (Container container)
{
Dimension size; // Minimum size of current component
int scaledWidth = 0; // Minimum width of scalled components
int scaledHeight = 0; // Minimum 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;
for (counter = 0; counter < columnSpec.length; counter++)
if ((columnSpec[counter] > 0.0) && (columnSpec[counter] < 1.0))
fillWidthRatio -= columnSpec[counter];
else if (columnSpec[counter] == FILL)
numFillWidth++;
for (counter = 0; counter < rowSpec.length; counter++)
if ((rowSpec[counter] > 0.0) && (rowSpec[counter] < 1.0))
fillHeightRatio -= rowSpec[counter];
else if (rowSpec[counter] == FILL)
numFillHeight++;
// Adjust fill ratios to reflect number of fill rows/columns
if (numFillWidth > 1)
fillWidthRatio /= numFillWidth;
if (numFillHeight > 1)
fillHeightRatio /= numFillHeight;
// Cap fill ratio bottoms to 0.0
if (fillWidthRatio < 0.0)
fillWidthRatio = 0.0;
if (fillHeightRatio < 0.0)
fillHeightRatio = 0.0;
// Find maximum minimum size of all scaled components
ListIterator iterator = list.listIterator(0);
while (iterator.hasNext())
{
// Get next entry
Entry entry = (Entry) iterator.next();
// Make sure entry is in valid rows and columns
if ((entry.col1 < 0) || (entry.col1 >= columnSpec.length) ||
(entry.col2 >= columnSpec.length) ||
(entry.row1 < 0) || (entry.row1 >= rowSpec.length) ||
(entry.row2 >= rowSpec.length))
{
// Skip the bad component
continue;
}
// Get minimum size of current component
size = entry.component.getMinimumSize();
// Calculate portion of component that is not absolutely sized
int scalableWidth = size.width;
int scalableHeight = size.height;
for (counter = entry.col1; counter <= entry.col2; counter++)
if (columnSpec[counter] >= 1.0)
scalableWidth -= columnSpec[counter];
for (counter = entry.row1; counter <= entry.row2; counter++)
if (rowSpec[counter] >= 1.0)
scalableHeight -= rowSpec[counter];
//----------------------------------------------------------------------
// Determine total percentage of scalable space that the component
// occupies by adding the relative columns and the fill columns
double relativeWidth = 0.0;
for (counter = entry.col1; counter <= entry.col2; counter++)
{
// Column is scaled
if ((columnSpec[counter] > 0.0) && (columnSpec[counter] < 1.0))
// Add scaled size to relativeWidth
relativeWidth += columnSpec[counter];
// Column is fill
else if ((columnSpec[counter] == FILL) && (fillWidthRatio != 0.0))
// Add fill size to relativeWidth
relativeWidth += fillWidthRatio;
}
// Determine the total scaled width as estimated by this component
if (relativeWidth == 0)
temp = 0;
else
temp = (int) (scalableWidth / relativeWidth + 0.5);
// If the container needs to be bigger, make it so
if (scaledWidth < temp)
scaledWidth = temp;
//----------------------------------------------------------------------
// Determine total percentage of scalable space that the component
// occupies by adding the relative columns and the fill columns
double relativeHeight = 0.0;
for (counter = entry.row1; counter <= entry.row2; counter++)
{
// Row is scaled
if ((rowSpec[counter] > 0.0) && (rowSpec[counter] < 1.0))
// Add scaled size to relativeHeight
relativeHeight += rowSpec[counter];
// Row is fill
else if ((rowSpec[counter] == FILL) && (fillHeightRatio != 0.0))
// Add fill size to relativeHeight
relativeHeight += fillHeightRatio;
}
// Determine the total scaled width as estimated by this component
if (relativeHeight == 0)
temp = 0;
else
temp = (int) (scalableHeight / relativeHeight + 0.5);
// If the container needs to be bigger, make it so
if (scaledHeight < temp)
scaledHeight = temp;
}
// totalWidth is the scaledWidth plus the sum of all absolute widths and all
// preferred widths
int totalWidth = scaledWidth;
for (counter = 0; counter < columnSpec.length; counter++)
// Is the current column an absolute size
if (columnSpec[counter] >= 1.0)
totalWidth += (int) (columnSpec[counter] + 0.5);
// Is the current column a preferred size
else if ((columnSpec[counter] == PREFERRED) ||
(columnSpec[counter] == MINIMUM))
{
// Assume a maximum width of zero
int maxWidth = 0;
// Find maximum preferred width of all components completely
// contained within this column
iterator = list.listIterator(0);
while (iterator.hasNext())
{
Entry entry = (Entry) iterator.next();
if ((entry.col1 == counter) && (entry.col2 == counter))
{
Dimension p = (columnSpec[counter] == PREFERRED) ?
entry.component.getPreferredSize() :
entry.component.getMinimumSize();
int width = (p == null) ? 0 : p.width;
if (maxWidth < width)
maxWidth = width;
}
}
// Add preferred width
totalWidth += maxWidth;
}
// totalHeight is the scaledHeight plus the sum of all absolute heights and
// all preferred widths
int totalHeight = scaledHeight;
for (counter = 0; counter < rowSpec.length; counter++)
// Is the current row an absolute size
if (rowSpec[counter] >= 1.0)
totalHeight += (int) (rowSpec[counter] + 0.5);
// Is the current row a preferred size
else if ((rowSpec[counter] == PREFERRED) ||
(rowSpec[counter] == MINIMUM))
{
// Assume a maximum height of zero
int maxHeight = 0;
// Find maximum preferred height of all components completely
// contained within this row
iterator = list.listIterator(0);
while (iterator.hasNext())
{
Entry entry = (Entry) iterator.next();
if ((entry.row1 == counter) && (entry.row1 == counter))
{
Dimension p = (rowSpec[counter] == PREFERRED) ?
entry.component.getPreferredSize() :
entry.component.getMinimumSize();
int height = (p == null) ? 0 : p.height;
if (maxHeight < height)
maxHeight = height;
}
}
// Add preferred height
totalHeight += maxHeight;
}
// Compensate for container's insets
Insets inset = container.getInsets();
totalWidth += inset.left + inset.right;
totalHeight += inset.top + inset.bottom;
return new Dimension(totalWidth, totalHeight);
}
/**
* Adds the specified component with the specified name to the layout.
*
* @param name indicates entry's position and anchor
* @param component component to add
*/
public void addLayoutComponent (String name, Component component)
{
addLayoutComponent (component, name);
}
//******************************************************************************
//** java.awt.event.LayoutManager2 methods ***
//******************************************************************************
/**
* Adds the specified component with the specified name to the layout.
*
* @param component component to add
* @param constraint indicates entry's position and alignment
*/
public void addLayoutComponent (Component component, Object constraint)
{
if (constraint instanceof String)
{
// Create an entry to associate component with its constraints
constraint = new TableLayoutConstraints((String) constraint);
// Add component and constraints to the list
list.add (new Entry(component, (TableLayoutConstraints) constraint));
}
else if (constraint instanceof TableLayoutConstraints)
{
// Add component and constraints to the list
list.add (new Entry(component, (TableLayoutConstraints) constraint));
}
else if (constraint == null)
throw new IllegalArgumentException("No constraint for the component");
else
throw new IllegalArgumentException
("Cannot accept a constraint of class " + constraint.getClass());
}
/**
* Removes the specified component from the layout.
*
* @param component component being removed
*/
public void removeLayoutComponent (Component component)
{
list.remove (component);
}
/**
* 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
*
* @return unconditionally, a Dimension of Integer.MAX_VALUE by
* Integer.MAX_VALUE since TableLayout does not limit the
* maximum size of a container
*/
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.
*
* @return unconditionally, 0.5
*/
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 wher
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -