下面列出了java.awt.Container#getComponent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Dimension preferredLayoutSize(Container target) {
synchronized (target.getTreeLock()) {
Dimension dim = new Dimension(0, 0);
for (int i = 0; i < target.getComponentCount(); i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = m.getPreferredSize();
// original
// dim.height = Math.max(dim.height, d.height);
// if (i > 0) { dim.width += hgap; }
// dim.width += d.width;
// new way
Point p = m.getLocation();
dim.width = Math.max(dim.width, p.x + d.width);
dim.height = Math.max(dim.height, p.y + d.height);
}
}
Insets insets = target.getInsets();
dim.width += insets.left + insets.right + getHgap() * 2;
dim.height += insets.top + insets.bottom + getVgap() * 2;
return dim;
}
}
private void addTree(Collection order, Set set, Container cont) {
for (int i = 0; i < cont.getComponentCount(); i++) {
Component comp = cont.getComponent(i);
ComponentPeer peer = comp.getPeer();
if (peer instanceof XComponentPeer) {
Long window = Long.valueOf(((XComponentPeer)peer).getWindow());
if (!set.contains(window)) {
set.add(window);
order.add(window);
}
} else if (comp instanceof Container) {
// It is lightweight container, it might contain heavyweight components attached to this
// peer
addTree(order, set, (Container)comp);
}
}
}
/**
* places the components defined by first to last within the target
* container using the bounds box defined.
*
* @param target the container.
* @param x the x coordinate of the area.
* @param y the y coordinate of the area.
* @param width the width of the area.
* @param height the height of the area.
* @param first the first component of the container to place.
* @param last the last component of the container to place.
*/
private void placethem(Container target, int x, int y, int width, int height,
int first, int last) {
int align = getAlignment();
if (align == MIDDLE)
y += height / 2;
if (align == BOTTOM)
y += height;
for (int i = first; i < last; i++) {
Component m = target.getComponent(i);
Dimension md = m.getSize();
if (m.isVisible()) {
int px = x + (width - md.width) / 2;
m.setLocation(px, y);
y += vgap + md.height;
}
}
}
private static Collection<JInternalFrame> getAllFrames(Container parent) {
int i, count;
Collection<JInternalFrame> results = new LinkedHashSet<>();
count = parent.getComponentCount();
for (i = 0; i < count; i++) {
Component next = parent.getComponent(i);
if (next instanceof JInternalFrame) {
results.add((JInternalFrame) next);
} else if (next instanceof JInternalFrame.JDesktopIcon) {
JInternalFrame tmp = ((JInternalFrame.JDesktopIcon) next).getInternalFrame();
if (tmp != null) {
results.add(tmp);
}
} else if (next instanceof Container) {
results.addAll(getAllFrames((Container) next));
}
}
return results;
}
private void addTree(Collection order, Set set, Container cont) {
for (int i = 0; i < cont.getComponentCount(); i++) {
Component comp = cont.getComponent(i);
ComponentPeer peer = comp.getPeer();
if (peer instanceof XComponentPeer) {
Long window = Long.valueOf(((XComponentPeer)peer).getWindow());
if (!set.contains(window)) {
set.add(window);
order.add(window);
}
} else if (comp instanceof Container) {
// It is lightweight container, it might contain heavyweight components attached to this
// peer
addTree(order, set, (Container)comp);
}
}
}
/**
* Returns the minimum size needed to layout the target container.
*
* @param target the component to lay out.
* @return the minimum layout dimension.
*/
@Override
public Dimension minimumLayoutSize(Container target) {
Dimension tarsiz = new Dimension(0, 0);
for (int i = 0; i < target.getComponentCount(); i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = m.getMinimumSize();
tarsiz.width = Math.max(tarsiz.width, d.width);
if (i > 0) {
tarsiz.height += vgap;
}
tarsiz.height += d.height;
}
}
Insets insets = target.getInsets();
tarsiz.width += insets.left + insets.right + hgap * 2;
tarsiz.height += insets.top + insets.bottom + vgap * 2;
return tarsiz;
}
@Override
public void layoutContainer(Container parent) {
int divider = getDivider(parent);
Insets insets = parent.getInsets();
int w = parent.getWidth() - insets.left - insets.right - divider;
int x = insets.left;
int y = insets.top;
for (int k = 1; k < parent.getComponentCount(); k += 2) {
Component comp1 = parent.getComponent(k - 1);
Component comp2 = parent.getComponent(k);
Dimension d = comp2.getPreferredSize();
comp1.setBounds(x, y, divider - hGap, d.height);
comp2.setBounds(x + divider, y, w, d.height);
y += d.height + vGap;
}
}
/**
* Traverses the children and determines row heights and column widths.
* @param parent the component which needs to be laid out
* @param min if true, the minimum size is used. Otherwise, the preferred size
* is used.
*/
protected void getGridSizes(Container parent, boolean min) {
int ncomponents = parent.getComponentCount();
if (ncomponents == 0) return;
int nrows = rows, ncols = cols;
if (nrows > 0)
ncols = (ncomponents + nrows - 1) / nrows;
else
nrows = (ncomponents + ncols - 1) / ncols;
row_heights = new int[nrows];
col_widths = new int[ncols];
for (int i = 0; i < ncomponents; i++) {
Component comp = parent.getComponent(i);
Dimension d = min ? comp.getMinimumSize() :
comp.getPreferredSize();
int row = i / ncols;
if (d.height > row_heights[row])
row_heights[row] = d.height;
int col = i % ncols;
if (d.width > col_widths[col])
col_widths[col] = d.width;
}
}
public Dimension getSize(Container parent, boolean minimum) {
int n = parent.getComponentCount();
Insets insets = parent.getInsets();
Dimension d = new Dimension();
for (int i = 0; i < n; i++) {
Component comp = parent.getComponent(i);
if (comp instanceof EnableButton) {
continue;
}
Dimension p = (minimum
? comp.getMinimumSize()
: comp.getPreferredSize());
if (horizontal) {
d.width += p.width;
if (d.height < p.height) {
d.height = p.height;
}
} else {
if (d.width < p.width) {
d.width = p.width;
}
d.height += p.height;
}
}
d.width += (insets.left + insets.right);
d.height += (insets.top + insets.bottom);
return d;
}
public void layoutContainer(Container parent) {
setParent(parent);
int n = parent.getComponentCount();
getConstraints(parent).reset();
for (int i = 0 ; i < n ; i++) {
getConstraints(parent.getComponent(i)).reset();
}
Insets insets = parent.getInsets();
Constraints pc = getConstraints(parent);
abandonCycles(pc.getX()).setValue(0);
abandonCycles(pc.getY()).setValue(0);
abandonCycles(pc.getWidth()).setValue(parent.getWidth() -
insets.left - insets.right);
abandonCycles(pc.getHeight()).setValue(parent.getHeight() -
insets.top - insets.bottom);
for (int i = 0 ; i < n ; i++) {
Component c = parent.getComponent(i);
Constraints cc = getConstraints(c);
int x = abandonCycles(cc.getX()).getValue();
int y = abandonCycles(cc.getY()).getValue();
int width = abandonCycles(cc.getWidth()).getValue();
int height = abandonCycles(cc.getHeight()).getValue();
c.setBounds(insets.left + x, insets.top + y, width, height);
}
}
public void layoutContainer(Container parent) {
setParent(parent);
int n = parent.getComponentCount();
getConstraints(parent).reset();
for (int i = 0 ; i < n ; i++) {
getConstraints(parent.getComponent(i)).reset();
}
Insets insets = parent.getInsets();
Constraints pc = getConstraints(parent);
abandonCycles(pc.getX()).setValue(0);
abandonCycles(pc.getY()).setValue(0);
abandonCycles(pc.getWidth()).setValue(parent.getWidth() -
insets.left - insets.right);
abandonCycles(pc.getHeight()).setValue(parent.getHeight() -
insets.top - insets.bottom);
for (int i = 0 ; i < n ; i++) {
Component c = parent.getComponent(i);
Constraints cc = getConstraints(c);
int x = abandonCycles(cc.getX()).getValue();
int y = abandonCycles(cc.getY()).getValue();
int width = abandonCycles(cc.getWidth()).getValue();
int height = abandonCycles(cc.getHeight()).getValue();
c.setBounds(insets.left + x, insets.top + y, width, height);
}
}
private void commitTableEditors(Container con) {
for (int i = 0; i < con.getComponentCount(); i++) {
if (con.getComponent(i) instanceof Container)
commitTableEditors((Container) con.getComponent(i));
final Component container = con.getComponent(i);
commitComponent(container);
if (container instanceof JScrollPane) {
commitComponent(((JScrollPane) container).getViewport()
.getView());
}
}
}
private void enableComponents(Container root, boolean enabled) {
root.setEnabled(enabled);
for (int i = 0; i < root.getComponentCount(); i++) {
Component child = root.getComponent(i);
if (child instanceof Container) {
enableComponents((Container)child, enabled);
} else {
child.setEnabled(enabled);
}
}
}
private void setEditsAction(final Container con) {
if (con instanceof JSpinner)
return;
/*
* if(con instanceof JSplitPane){
* setEditsAction(((JSplitPane)con).getLeftComponent());
* setEditsAction(((JSplitPane)con).getRightComponent()); return; }
*/
for (int i = 0; i < con.getComponentCount(); i++) {
if (con.getComponent(i) instanceof Container)
setEditsAction((Container) con.getComponent(i));
final Component container = con.getComponent(i);
if (container instanceof JTextField) {
processTextField((JTextField) container);
} else if (container instanceof JPasswordField) {
((JPasswordField) container).addActionListener(listener);
}
if (container instanceof JTextComponent) {
addUndoFunctions((JTextComponent) container);
}
if (container instanceof JList
&& !(container instanceof JTableHeader)) {
((JList) container).addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(final MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1
&& e.getClickCount() > 1)
onOk();
}
});
}
}
}
/**
* 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;
}
}
/**
* 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(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;
}
}
private static Component getC(Component component, int index) {
Container container = (Container) component;
return container.getComponent(index);
}
private static SpringLayout.Constraints getConstraintsForCell(int r, int c, Container parent, int cols) {
SpringLayout layout = (SpringLayout) parent.getLayout();
Component component = parent.getComponent(r * cols + c);
return layout.getConstraints(component);
}
@Override
public Dimension preferredLayoutSize(Container parent) {
Component child = parent.getComponent(0);
Dimension d = child.getPreferredSize();
return getRotation().transform(d);
}