/** * Returns the {@link Constraints} object associated with the specified component. * * @param c the component for which to determine the constraint. * @return the {@link Constraints} object associated with the specified component. */ public SpringLayout.Constraints getConstraints(Component c) { Constraints constraints = (Constraints) constraintsMap.get(c); if (constraints == null) { Container parent = c.getParent(); constraints = new Constraints(); if (parent != null) { constraints.setX(Spring.constant(parent.getInsets().left)); constraints.setY(Spring.constant(parent.getInsets().top)); } else { constraints.setX(Spring.constant(0)); constraints.setY(Spring.constant(0)); } } constraints.setWidth( Spring.constant( c.getMinimumSize().width, c.getPreferredSize().width, c.getMaximumSize().width)); constraints.setHeight( Spring.constant( c.getMinimumSize().height, c.getPreferredSize().height, c.getMaximumSize().height)); constraintsMap.put(c, constraints); return constraints; }
protected void enforceMinimumAndMaximumWidgetSizes(java.awt.Component component) { java.awt.Dimension size = component.getSize(); boolean changed = false; // enforce minimum java.awt.Dimension minimumSize = component.getMinimumSize(); if (size.width < minimumSize.width) { size.width = minimumSize.width; changed = true; } if (size.height < minimumSize.height) { size.height = minimumSize.height; changed = true; } // enforce maximum java.awt.Dimension maximumSize = component.getMaximumSize(); if (maximumSize != null) { if (size.width > maximumSize.width && maximumSize.width > 0) { size.width = maximumSize.width; changed = true; } if (size.height > maximumSize.height && maximumSize.height > 0) { size.height = maximumSize.height; changed = true; } } if (changed) { component.setSize(size); } }
/** * Calculates the maximum size dimensions for the specified panal given the components in the * specified parent container. */ public Dimension maximumLayoutSize(Container target) { synchronized (target.getTreeLock()) { Dimension dim = new Dimension(0, 0); int size = actions.size(); if ((grip != null) && grip.isVisible()) { Dimension d = grip.getPreferredSize(); dim.width += d.width; dim.width += hgap; } Component last = null; for (int i = 0; i < size; i++) { Component comp = (Component) actions.elementAt(i); if (comp.isVisible()) { Dimension d = comp.getPreferredSize(); dim.width += d.width; dim.height = Math.max(dim.height, d.height); dim.width += hgap; last = comp; } } if (last != null) { Dimension prefSize = last.getPreferredSize(); Dimension maxSize = last.getMaximumSize(); if (prefSize != maxSize) { dim.width = dim.width - prefSize.width + maxSize.width; dim.height = Math.max(dim.height, maxSize.height); } } Insets insets = target.getInsets(); dim.width += insets.left + insets.right; dim.height += insets.top + insets.bottom; return dim; } }
/** @see java.awt.event.ComponentAdapter#componentResized(java.awt.event.ComponentEvent) */ public void componentResized(ComponentEvent evt) { /* 获取目标组件 */ Component component = evt.getComponent(); /* 获取目标组件设置的边界和最小最大尺寸 */ Rectangle bounds = component.getBounds(); Dimension minSize = component.getMinimumSize(); Dimension maxSize = component.getMaximumSize(); /* 确定目标组件新的x轴坐标及宽度 */ if (bounds.width < minSize.width) { bounds.x -= (bounds.x == m_oldBounds.x ? 0 : minSize.width - bounds.width); bounds.width = minSize.width; } else if (bounds.width > maxSize.width) { bounds.x += (bounds.x == m_oldBounds.x ? 0 : bounds.width - maxSize.width); bounds.width = maxSize.width; } /* 确定目标组件新的y轴坐标及高度 */ if (bounds.height < minSize.height) { bounds.y -= (bounds.y == m_oldBounds.y ? 0 : minSize.height - bounds.height); bounds.height = minSize.height; } else if (bounds.height > maxSize.height) { bounds.y += (bounds.y == m_oldBounds.y ? 0 : bounds.height - maxSize.height); bounds.height = maxSize.height; } /* 设置目标组件的新边界 */ component.setBounds(bounds); /* 保存新的边界 */ m_oldBounds = bounds; }
public void layoutContainer(Container parent) { int n = rows.size(); Insets border = parent.getInsets(); int lastY = border.top; // figure out where the second column should start int secondColumn = 0; for (int i = 0; i < n; i++) { Row r = (Row) rows.get(i); if (r instanceof NormalRow) { NormalRow nr = (NormalRow) r; secondColumn = Math.max( secondColumn, // nr.l.getMinimumSize().width); nr.getLabelWidth(false)); } } secondColumn += border.left + EXTRA; for (int i = 0; i < n; i++) { Row r = (Row) rows.get(i); if (r instanceof HeaderRow) { // header Component header = ((HeaderRow) r).h; int w = parent.getWidth() - (border.left + border.right); int h = header.getMinimumSize().height; int x = border.left; int y = lastY; header.setBounds(x, y, w, h); lastY += h; } else if (r instanceof NormalRow) { // label JLabel l = (JLabel) ((NormalRow) r).l; int w1 = l.getMinimumSize().width; // int h1 = l.getPreferredSize().height; int h1 = l.getMinimumSize().height; int x1 = secondColumn - w1; int y1 = lastY; l.setBounds(x1, y1, w1, h1); // control Component c = ((NormalRow) r).c; // WAS: int w2 = c.getPreferredSize().width; int w2 = c.getMaximumSize().width; // NEW! w2 = Math.min(w2, parent.getWidth() - (border.right + PADDING + secondColumn)); int h2 = c.getPreferredSize().height; int x2 = secondColumn + PADDING; // buffer between 'em int y2 = lastY; c.setBounds(x2, y2, w2, h2); lastY += Math.max(h1, h2); } } // focus on the first one? (is that my job?) (is it even possible?) }
/** * @param dim * @param cc */ private Dimension rozmer(final Dimension dim, final Component cc) { final Dimension prs = cc.getPreferredSize(); final Dimension mis = cc.getMinimumSize(); final Dimension mas = cc.getMaximumSize(); final int dx = rozmer(ratiox, dim.width, prs.width, mis.width, mas.width); final int dy = rozmer(ratioy, dim.height, prs.height, mis.height, mas.height); final Dimension v = new Dimension(dx, dy); return v; }
/** Lays out the container in the specified panel. */ public void layoutContainer(Container target) { synchronized (target.getTreeLock()) { Insets insets = target.getInsets(); Dimension dim = target.getSize(); int fullHeight = dim.height; int bottom = dim.height - insets.bottom - 1; int top = 0 + insets.top; int left = insets.left; int maxPosition = dim.width - (insets.left + insets.right) - hgap; int right = dim.width - insets.right; maxPosition = right; int height = bottom - top; int size = actions.size(); int w, h; if ((grip != null) && grip.isVisible()) { Dimension d = grip.getPreferredSize(); grip.setBounds(left, top + vgap, d.width, bottom - top - 2 * vgap); left += d.width; left += hgap; } for (int i = 0; i < size; i++) { left += hgap; Component comp = (Component) actions.elementAt(i); Dimension d; Dimension minSize = comp.getMinimumSize(); if (i == size - 1) d = comp.getMaximumSize(); else d = comp.getPreferredSize(); w = d.width; h = Math.min(height, d.height); if ((left < maxPosition) && (left + w > maxPosition)) { if (maxPosition - left >= minSize.width) w = maxPosition - left; } comp.setBounds(left, (fullHeight - d.height) / 2, w, h); // if (i == size - 1) // d = comp.getMaximumSize(); // else // d = comp.getPreferredSize(); // if ((left + d.width) > right) // w = right - left; // else // w = d.width; // h = Math.min (height, d.height); // comp.setBounds (left, (fullHeight - d.height)/2, w, h); left += d.width; } } }
/** * Resizes the <code>TableColumn</code> to fit the width of its header cell. This method does * nothing if the header renderer is <code>null</code> (the default case). Otherwise, it sets the * minimum, maximum and preferred widths of this column to the widths of the minimum, maximum and * preferred sizes of the Component delivered by the header renderer. The transient "width" * property of this TableColumn is also set to the preferred width. Note this method is not used * internally by the table package. * * @see #setPreferredWidth */ public void sizeWidthToFit() { if (headerRenderer == null) { return; } Component c = headerRenderer.getTableCellRendererComponent(null, getHeaderValue(), false, false, 0, 0); setMinWidth(c.getMinimumSize().width); setMaxWidth(c.getMaximumSize().width); setPreferredWidth(c.getPreferredSize().width); setWidth(getPreferredWidth()); }
@Override public Dimension maximumLayoutSize(Container parent) { if (parent.getComponentCount() == 0) { return new Dimension(0, 0); } else { int x = -1, y = -1; for (Component child : parent.getComponents()) { Dimension dim = child.getMaximumSize(); x = Math.max(dim.width, x); y = Math.max(dim.height, y); } return new Dimension(x, y); } }
/** * Lays out the container in the specified container. * * @see java.awt.LayoutManager#layoutContainer(java.awt.Container) */ public void layoutContainer(Container cont) { int numComps = cont.getComponentCount(); if (numComps <= 0) return; calcLayoutSize(cont); Insets insets = cont.getInsets(); int x = Math.max(insets.left, (cont.getWidth() - minWidth) / 2); for (int i = 0; i < numComps; i++) { Component c = cont.getComponent(i); Dimension prfDim = c.getPreferredSize(); Dimension maxDim = c.getMaximumSize(); int y; if (maxDim.getHeight() >= cont.getHeight()) { y = 0; } else { float baseline; if (c instanceof FormulaElement) baseline = ((FormulaElement) c).getBaseline(); else baseline = (float) prfDim.getHeight() * c.getAlignmentY(); y = Math.round(maxAscend - baseline) + insets.top; } int w = prfDim.width; int h = Math.min(maxDim.height, cont.getHeight()); c.setBounds(x, y, w, h); x += w; if (i < numComps - 1) { x += hgap; Component cNext = cont.getComponent(i + 1); if ((c instanceof FormulaElement) && (cNext instanceof FormulaElement)) x += getSpacing((FormulaElement) c, (FormulaElement) cNext); } } }
/** * A debugging utility that prints to stdout the component's minimum, preferred, and maximum * sizes. */ public static void printSizes(Component c) { System.out.println("minimumSize = " + c.getMinimumSize()); System.out.println("preferredSize = " + c.getPreferredSize()); System.out.println("maximumSize = " + c.getMaximumSize()); }
/* * (non-Javadoc) * * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container) */ @Override public Dimension minimumLayoutSize(final Container aArg0) { return podklad.getMaximumSize(); }
@Override public void layoutContainer(Container target) { synchronized (target.getTreeLock()) { Insets insets = target.getInsets(); int maxwidth = target.getWidth() - (insets.left + insets.right + getHgap() * 2); int nmembers = target.getComponentCount(); int x = 0, y = insets.top + getVgap(); int rowh = 0, start = 0; boolean ltr = target.getComponentOrientation().isLeftToRight(); SizeRequirements[] xChildren = new SizeRequirements[nmembers]; SizeRequirements[] yChildren = new SizeRequirements[nmembers]; for (int i = 0; i < nmembers; i++) { Component c = target.getComponent(i); if (!c.isVisible()) { xChildren[i] = new SizeRequirements(0, 0, 0, c.getAlignmentX()); yChildren[i] = new SizeRequirements(0, 0, 0, c.getAlignmentY()); } else { Dimension min = c.getMinimumSize(); Dimension typ = c.getPreferredSize(); Dimension max = c.getMaximumSize(); xChildren[i] = new SizeRequirements(min.width, typ.width, max.width, c.getAlignmentX()); yChildren[i] = new SizeRequirements(min.height, typ.height, max.height, c.getAlignmentY()); if ((x == 0) || ((x + typ.width) <= maxwidth)) { if (x > 0) { x += getHgap(); } x += typ.width; rowh = Math.max(rowh, typ.height); } else { layoutComponents( target, insets.left + getHgap(), y, maxwidth, rowh, xChildren, yChildren, start, i, ltr); x = typ.width; y += getVgap() + rowh; rowh = typ.height; start = i; } } } layoutComponents( target, insets.left + getHgap(), y, maxwidth, rowh, xChildren, yChildren, start, nmembers, ltr); } }