public void layoutContainer(Container parent) { Dimension ourSize = preferredLayoutSize(parent), // Does the following do something useful? // And shouldn't we cater to real instead of maximum sizes? // This way, if a VerticallyLaidout component had a particular size forced upon it, // wouldn't we neglect to scale down to using minimum sizes so long as its maximum // size was bigger than our preferred? all: [Davis] maxSize = parent.getMaximumSize(), size = parent.getSize(); Insets isets = parent.getInsets(); Dimension compSize; int n = parent.getComponentCount(); int y; boolean usingPreferredSize = true; if (ourSize.width > maxSize.width || ourSize.height > maxSize.height) { ourSize = minimumLayoutSize(parent); usingPreferredSize = false; } switch (verticalAlignment) { case TOP: y = isets.top; break; case BOTTOM: y = (size.height - ourSize.height); break; default: // assume MIDDLE y = (size.height - ourSize.height) / 2 + isets.top; break; } for (int i = 0; i < n; i++) { Component c = parent.getComponent(i); compSize = usingPreferredSize ? c.getPreferredSize() : c.getMinimumSize(); switch (horizontalAlignment) { case LEFT: c.setBounds(isets.left, y, compSize.width, compSize.height); break; case RIGHT: c.setBounds( ourSize.width - isets.right - compSize.width + isets.left, y, compSize.width, compSize.height); break; default: // assume CENTER c.setBounds( (ourSize.width - isets.left - isets.right - compSize.width + isets.left) / 2, y, compSize.width, compSize.height); } y += compSize.height + vgap; } }
@Override public Dimension maximumLayoutSize(Container target) { return target.getMaximumSize(); }