下面列出了java.awt.Container#getTreeLock ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Dimension computeMinSize(Container target) {
synchronized (target.getTreeLock()) {
int minx = Integer.MAX_VALUE;
int miny = Integer.MIN_VALUE;
boolean foundOne = false;
int n = target.getComponentCount();
for (int i = 0; i < n; i++) {
Component c = target.getComponent(i);
if (c.isVisible()) {
foundOne = true;
Dimension d = c.getPreferredSize();
minx = Math.min(minx, d.width);
miny = Math.min(miny, d.height);
}
}
if (foundOne) {
return new Dimension(minx, miny);
}
return new Dimension(0, 0);
}
}
/**
* Returns the preferred size.
*
* @param parent
* the parent.
* @return the preferred size.
*/
public Dimension preferredLayoutSize( final Container parent ) {
synchronized ( parent.getTreeLock() ) {
final Insets insets = parent.getInsets();
if ( parent.getComponentCount() > 0 ) {
final Component component = parent.getComponent( 0 );
final Dimension d = component.getPreferredSize();
return new Dimension( (int) d.getWidth() + insets.left + insets.right, (int) d.getHeight() + insets.top
+ insets.bottom );
} else {
return new Dimension( insets.left + insets.right, insets.top + insets.bottom );
}
}
}
/**
* Calculates the minimum size dimensions for the specified
* container, given the components it contains.
*
* @param parent the component to be laid out
* @return the minimum size computed for the parent.
* @see #preferredLayoutSize
*/
public Dimension minimumLayoutSize(final Container parent) {
synchronized (parent.getTreeLock()) {
final Insets ins = parent.getInsets();
final Component[] comps = parent.getComponents();
int height = 0;
int width = 0;
for (int i = 0; i < comps.length; i++) {
if ((comps[i].isVisible() == false) && this.ignoreInvisible) {
continue;
}
final Dimension pref = comps[i].getMinimumSize();
if (pref.height > height) {
height = pref.height;
}
if (pref.width > width) {
width = pref.width;
}
}
return new Dimension(width + ins.left + ins.right,
height + ins.top + ins.bottom);
}
}
/**
* Calculates the minimum size dimensions for the specified
* panel given the components in the specified parent container.
*
* @param parent the component to be laid out
* @return the minimul layoutsize
* @see #preferredLayoutSize
*/
public Dimension minimumLayoutSize(final Container parent) {
synchronized (parent.getTreeLock()) {
final Insets ins = parent.getInsets();
final Component[] comps = parent.getComponents();
int height = ins.top + ins.bottom;
int width = ins.left + ins.right;
for (int i = 0; i < comps.length; i++) {
if (comps[i].isVisible() == false) {
continue;
}
final Dimension min = comps[i].getMinimumSize();
height += min.height;
if (min.width > width) {
width = min.width;
}
}
return new Dimension(width + ins.left + ins.right,
height + ins.top + ins.bottom);
}
}
/**
* Description of the Method
*
*@param target Description of Parameter
*@return Description of the Returned Value
*/
public Dimension minimumLayoutSize(Container target) {
synchronized (target.getTreeLock()) {
Dimension dim = new Dimension(0, 0);
int nmembers = target.getComponentCount();
boolean firstVisibleComponent = true;
for (int ii = 0; ii < nmembers; ii++) {
Component m = target.getComponent(ii);
if (m.isVisible()) {
Dimension d = m.getPreferredSize();
dim.width = Math.max(dim.width, d.width);
if (firstVisibleComponent) {
firstVisibleComponent = false;
}
else {
dim.height += _vgap;
}
dim.height += d.height;
}
}
Insets insets = target.getInsets();
dim.width += insets.left + insets.right + _hgap * 2;
dim.height += insets.top + insets.bottom + _vgap * 2;
return dim;
}
}
@Override
public Dimension preferredLayoutSize(Container parent) {
//System.err.println("preferredLayoutSize");
synchronized (parent.getTreeLock()) {
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
int nrows = getRows();
int ncols = getColumns();
if (nrows > 0) {
ncols = (ncomponents + nrows - 1) / nrows;
} else {
nrows = (ncomponents + ncols - 1) / ncols;
}
int[] w = new int[ncols];
int[] h = new int[nrows];
for (int i = 0; i < ncomponents; i++) {
int r = i / ncols;
int c = i % ncols;
Component comp = parent.getComponent(i);
Dimension d = comp.getPreferredSize();
if (w[c] < d.width) {
w[c] = d.width;
}
if (h[r] < d.height) {
h[r] = d.height;
}
}
int nw = 0;
for (int j = 0; j < ncols; j++) {
nw += w[j];
}
int nh = 0;
for (int i = 0; i < nrows; i++) {
nh += h[i];
}
return new Dimension(insets.left + insets.right + nw + (ncols - 1) * getHgap(),
insets.top + insets.bottom + nh + (nrows - 1) * getVgap());
}
}
@Override
public Dimension minimumLayoutSize(Container parent)
{
synchronized (parent.getTreeLock())
{
return calculateSize(parent, Component::getMinimumSize);
}
}
/**
* Invalidates the layout, indicating that if the layout manager
* has cached information it should be discarded.
*
* @param parent the {@code Container} hosting this LayoutManager
* @throws IllegalArgumentException if {@code parent} is not
* the same {@code Container} that this was created with
*/
public void invalidateLayout(Container parent) {
checkParent(parent);
// invalidateLayout is called from Container.invalidate, which
// does NOT grab the treelock. All other methods do. To make sure
// there aren't any possible threading problems we grab the tree lock
// here.
synchronized(parent.getTreeLock()) {
isValid = false;
}
}
/**
* Computes and returns the horizontal and vertical grid origins. Performs the same layout
* process as {@code #layoutContainer} but does not layout the components.<p>
*
* This method has been added only to make it easier to debug the form layout. <strong>You must
* not call this method directly; It may be removed in a future release or the visibility may be
* reduced.</strong>
*
* @param parent the {@code Container} to inspect
* @return an object that comprises the grid x and y origins
*/
public LayoutInfo getLayoutInfo(Container parent) {
synchronized (parent.getTreeLock()) {
initializeColAndRowComponentLists();
Dimension size = parent.getSize();
Insets insets = parent.getInsets();
int totalWidth = size.width - insets.left - insets.right;
int totalHeight = size.height - insets.top - insets.bottom;
int[] x = computeGridOrigins(parent,
totalWidth, insets.left,
colSpecs,
colComponents,
colGroupIndices,
minimumWidthMeasure,
preferredWidthMeasure
);
int[] y = computeGridOrigins(parent,
totalHeight, insets.top,
rowSpecs,
rowComponents,
rowGroupIndices,
minimumHeightMeasure,
preferredHeightMeasure
);
return new LayoutInfo(x, y);
}
}
/**
* Returns the preferred size using this layout manager.
*
* @param parent the parent.
*
* @return the preferred size using this layout manager.
*/
public Dimension preferredLayoutSize(Container parent) {
synchronized (parent.getTreeLock()) {
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
int nrows = ncomponents / COLUMNS;
for (int c = 0; c < COLUMNS; c++) {
for (int r = 0; r < nrows; r++) {
Component component = parent.getComponent(r * COLUMNS + c);
Dimension d = component.getPreferredSize();
if (this.colWidth[c] < d.width) {
this.colWidth[c] = d.width;
}
if (this.rowHeight[r] < d.height) {
this.rowHeight[r] = d.height;
}
}
}
int totalHeight = this.vGap * (nrows - 1);
for (int r = 0; r < nrows; r++) {
totalHeight = totalHeight + this.rowHeight[r];
}
int totalWidth = this.colWidth[0] + this.labelGap
+ this.colWidth[1] + this.buttonGap + this.colWidth[2];
return new Dimension(insets.left + insets.right + totalWidth
+ this.labelGap + this.buttonGap, insets.top
+ insets.bottom + totalHeight + this.vGap);
}
}
@Override
public Dimension preferredLayoutSize(Container target) {
synchronized (target.getTreeLock()) {
applyLayout = false;
return doLayout(target);
}
}
@Override
public Dimension minimumLayoutSize(Container target)
{
synchronized (target.getTreeLock())
{
Dimension dim = new Dimension(0, 0);
int nmembers = target.getComponentCount();
Insets insets = target.getInsets();
int maxwidth = target.getWidth() - (insets.left + insets.right + getHgap() * 2);
int width = 0;
int height = 0;
int component = 0;
for (int i = 0; i < nmembers; i++, component++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = m.getMinimumSize();
if (component > 0)
{
if (width + d.width > maxwidth)
{
dim.width = Math.max(dim.width, width);
dim.height += height + getVgap();
width = 0;
height = 0;
component = 0;
}
width += getHgap();
}
height = Math.max(height, d.height);
width += d.width;
}
}
dim.width = Math.max(dim.width, width);
dim.height += height;
dim.width += insets.left + insets.right + getHgap() * 2;
dim.height += insets.top + insets.bottom + getVgap() * 2;
return dim;
}
}
@Override
public Dimension minimumLayoutSize(Container target)
{
synchronized (target.getTreeLock())
{
Dimension dim = new Dimension(0, 0);
int nmembers = target.getComponentCount();
Insets insets = target.getInsets();
int maxwidth = target.getWidth() - (insets.left + insets.right + getHgap() * 2);
int width = 0;
int height = 0;
int component = 0;
for (int i = 0; i < nmembers; i++, component++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = m.getMinimumSize();
if (component > 0)
{
if (width + d.width > maxwidth)
{
dim.width = Math.max(dim.width, width);
dim.height += height + getVgap();
width = 0;
height = 0;
component = 0;
}
width += getHgap();
}
height = Math.max(height, d.height);
width += d.width;
}
}
dim.width = Math.max(dim.width, width);
dim.height += height;
dim.width += insets.left + insets.right + getHgap() * 2;
dim.height += insets.top + insets.bottom + getVgap() * 2;
return dim;
}
}
/**
* Returns the minimum or preferred dimension needed to layout the target
* container.
*
* @param target target to get layout size for
* @param preferred should preferred size be calculated
* @return the dimension to layout the target container
*/
private Dimension layoutSize(Container target, boolean preferred) {
synchronized (target.getTreeLock()) {
// Each row must fit with the width allocated to the containter.
// When the container width = 0, the preferred width of the container
// has not yet been calculated so lets ask for the maximum.
int targetWidth = target.getSize().width;
if (targetWidth == 0) {
targetWidth = Integer.MAX_VALUE;
}
int hgap = getHgap();
int vgap = getVgap();
Insets insets = target.getInsets();
int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
int maxWidth = targetWidth - horizontalInsetsAndGap;
// Fit components into the allowed width
Dimension dim = new Dimension(0, 0);
int rowWidth = 0;
int rowHeight = 0;
int nmembers = target.getComponentCount();
for (int i = 0; i < nmembers; i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();
// Can't add the component to current row. Start a new row.
if (rowWidth + d.width > maxWidth) {
addRow(dim, rowWidth, rowHeight);
rowWidth = 0;
rowHeight = 0;
}
// Add a horizontal gap for all components after the first
if (rowWidth != 0) {
rowWidth += hgap;
}
rowWidth += d.width;
rowHeight = Math.max(rowHeight, d.height);
}
}
addRow(dim, rowWidth, rowHeight);
dim.width += horizontalInsetsAndGap;
dim.height += insets.top + insets.bottom + vgap * 2;
// When using a scroll pane or the DecoratedLookAndFeel we need to
// make sure the preferred size is less than the size of the
// target containter so shrinking the container size works
// correctly. Removing the horizontal gap is an easy way to do this.
Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);
if (scrollPane != null && target.isValid()) {
dim.width -= (hgap + 1);
}
return dim;
}
}
/**
* Returns the minimum or preferred dimension needed to layout the target
* container.
*
* @param target
* target to get layout size for
* @param preferred
* should preferred size be calculated
* @return the dimension to layout the target container
*/
private Dimension layoutSize(Container target, boolean preferred)
{
synchronized (target.getTreeLock())
{
// Each row must fit with the width allocated to the containter.
// When the container width = 0, the preferred width of the
// container
// has not yet been calculated so lets ask for the maximum.
int targetWidth = target.getSize().width;
if (targetWidth == 0)
targetWidth = Integer.MAX_VALUE;
final int hgap = getHgap();
final int vgap = getVgap();
final Insets insets = target.getInsets();
final int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
final int maxWidth = targetWidth - horizontalInsetsAndGap;
// Fit components into the allowed width
final Dimension dim = new Dimension(0, 0);
int rowWidth = 0;
int rowHeight = 0;
final int nmembers = target.getComponentCount();
for (int i = 0; i < nmembers; i++)
{
final Component m = target.getComponent(i);
if (m.isVisible())
{
final Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();
// Can't add the component to current row. Start a new row.
if (rowWidth + d.width > maxWidth)
{
addRow(dim, rowWidth, rowHeight);
rowWidth = 0;
rowHeight = 0;
}
// Add a horizontal gap for all components after the first
if (rowWidth != 0)
{
rowWidth += hgap;
}
rowWidth += d.width;
rowHeight = Math.max(rowHeight, d.height);
}
}
addRow(dim, rowWidth, rowHeight);
dim.width += horizontalInsetsAndGap;
dim.height += insets.top + insets.bottom + vgap * 2;
// When using a scroll pane or the DecoratedLookAndFeel we need to
// make sure the preferred size is less than the size of the
// target containter so shrinking the container size works
// correctly. Removing the horizontal gap is an easy way to do this.
final Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);
if (scrollPane != null && target.isValid())
{
dim.width -= (hgap + 1);
}
return dim;
}
}
@Override
public Dimension preferredLayoutSize(Container parent) {
synchronized (parent.getTreeLock()) {
return new Dimension(100, height);
}
}
/**
* Returns the minimum or preferred dimension needed to layout the target container.
*
* @param target target to get layout size for
* @param preferred should preferred size be calculated
* @return the dimension to layout the target container
*/
private Dimension layoutSize(final Container target, final boolean preferred) {
synchronized (target.getTreeLock()) {
// Each row must fit with the width allocated to the containter.
// When the container width = 0, the preferred width of the container
// has not yet been calculated, so use the FlowLayout implementation which
// lays everything out in a line. Also schedule a revalidation so that
// the actual size is realized.
final int targetWidth = target.getWidth();
if (targetWidth == 0) {
scheduleRevalidate(target);
return preferred ? super.preferredLayoutSize(target) : super.minimumLayoutSize(target);
}
final int hgap = getHgap();
final int vgap = getVgap();
final Insets insets = target.getInsets();
final int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
final int maxWidth = targetWidth - horizontalInsetsAndGap;
// Fit components into the allowed width
final Dimension dim = new Dimension(0, 0);
int rowWidth = 0;
int rowHeight = 0;
final int nmembers = target.getComponentCount();
for (int i = 0; i < nmembers; i++) {
final Component m = target.getComponent(i);
if (m.isVisible()) {
final Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();
// Can't add the component to current row. Start a new row.
if (rowWidth + d.width > maxWidth) {
addRow(dim, rowWidth, rowHeight);
rowWidth = 0;
rowHeight = 0;
}
// Add a horizontal gap for all components after the first
if (rowWidth != 0) {
rowWidth += hgap;
}
rowWidth += d.width;
rowHeight = Math.max(rowHeight, d.height);
}
}
addRow(dim, rowWidth, rowHeight);
dim.width += horizontalInsetsAndGap;
dim.height += insets.top + insets.bottom + vgap * 2;
// When using a scroll pane or the DecoratedLookAndFeel we need to make sure the preferred
// size is less than the size of the target containter so shrinking the container size works
// correctly. Removing the horizontal gap is an easy way to do this.
final Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);
if (scrollPane != null && target.isValid()) {
dim.width -= (hgap + 1);
}
return dim;
}
}
@Override
public Dimension maximumLayoutSize(Container parent) {
Scale scale = Scale.get(parent);
long height = 0;
long width = (long) (mColumns - 1) * scale.scale(mHGap);
synchronized (parent.getTreeLock()) {
Insets insets = parent.getInsets();
int compCount = parent.getComponentCount();
int[] widths = new int[mColumns];
int rows = 1 + (compCount - 1) / mColumns;
int i;
width += insets.left + insets.right;
for (int y = 0; y < rows; y++) {
int rowHeight = 0;
for (int x = 0; x < mColumns; x++) {
i = y * mColumns + x;
if (i < compCount) {
Dimension size = parent.getComponent(i).getMaximumSize();
if (size.height > rowHeight) {
rowHeight = size.height;
}
if (size.width > widths[x]) {
widths[x] = size.width;
}
}
}
height += rowHeight;
}
height += insets.top + insets.bottom;
if (rows > 0) {
height += (long) (rows - 1) * scale.scale(mVGap);
}
for (i = 0; i < mColumns; i++) {
width += widths[i];
}
}
if (width > LayoutSize.MAXIMUM_SIZE) {
width = LayoutSize.MAXIMUM_SIZE;
}
if (height > LayoutSize.MAXIMUM_SIZE) {
height = LayoutSize.MAXIMUM_SIZE;
}
return new Dimension((int) width, (int) height);
}
/**
* Returns the minimum or preferred dimension needed to layout the target
* container.
*
* @param target target to get layout size for
* @param preferred should preferred size be calculated
* @return the dimension to layout the target container
*/
private Dimension layoutSize(final Container target, final boolean preferred) {
synchronized (target.getTreeLock()) {
// Each row must fit with the width allocated to the containter.
// When the container width = 0, the preferred width of the
// container
// has not yet been calculated so lets ask for the maximum.
int targetWidth = target.getSize().width;
if (targetWidth == 0) {
targetWidth = Integer.MAX_VALUE;
}
int hgap = getHgap();
int vgap = getVgap();
Insets insets = target.getInsets();
int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
int maxWidth = targetWidth - horizontalInsetsAndGap;
// Fit components into the allowed width
Dimension dim = new Dimension(0, 0);
int rowWidth = 0;
int rowHeight = 0;
int nmembers = target.getComponentCount();
for (int i = 0; i < nmembers; i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();
// Can't add the component to current row. Start a new row.
if (rowWidth + d.width > maxWidth) {
addRow(dim, rowWidth, rowHeight);
rowWidth = 0;
rowHeight = 0;
}
// Add a horizontal gap for all components after the first
if (rowWidth != 0) {
rowWidth += hgap;
}
rowWidth += d.width;
rowHeight = Math.max(rowHeight, d.height);
}
}
addRow(dim, rowWidth, rowHeight);
dim.width += horizontalInsetsAndGap;
dim.height += insets.top + insets.bottom + vgap * 2;
// When using a scroll pane or the DecoratedLookAndFeel we need to
// make sure the preferred size is less than the size of the
// target containter so shrinking the container size works
// correctly. Removing the horizontal gap is an easy way to do this.
Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);
if (scrollPane != null) {
dim.width -= (hgap + 1);
}
return dim;
}
}
public Dimension preferredLayoutSize(Container parent) {
synchronized (parent.getTreeLock()) {
int width = 0;
int height = 0;
for (Component comp: components) {
Dimension size = comp.getPreferredSize();
width = Math.max(size.width, width);
height = Math.max(size.height, height);
}
Insets insets = parent.getInsets();
width += insets.left + insets.right;
height += insets.top + insets.bottom;
return new Dimension(width, height);
}
}