StackedLayout extends AbstractOrientedLayout { public void layout(Container container) { Dimension2D size = getPreferredSize(container); Rectangle2D bounds = container.getBounds(); Insets2D insets = container.getInsets(); double xMin = bounds.getMinX() + insets.getLeft(); double yMin = bounds.getMinY() + insets.getTop(); double width = bounds.getWidth() - insets.getLeft() - insets.getRight(); double height = bounds.getHeight() - insets.getTop() - insets.getBottom(); int count = 0; if (getOrientation() == Orientation.HORIZONTAL) { xMin += Math.max(bounds.getWidth() - size.getWidth(), 0.0)*defaultConstraints.getAlignmentX(); for (Drawable component : container) { if (count++ > 0) { xMin += getGapX(); } Dimension2D compBounds = component.getPreferredSize(); Constraints constraints = getConstraints(component, container); double componentHeight; double componentY; if (constraints.isStretched()) { componentHeight = height; componentY = yMin; } else { componentHeight = Math.min(compBounds.getHeight(), height); componentY = yMin + (height - componentHeight)*constraints.getAlignmentY(); } component.setBounds(xMin, componentY, compBounds.getWidth(), componentHeight); xMin += compBounds.getWidth(); } } else if (getOrientation() == Orientation.VERTICAL) { yMin += Math.max(bounds.getHeight() - size.getHeight(), 0.0)*defaultConstraints.getAlignmentY(); for (Drawable component : container) { if (count++ > 0) { yMin += getGapY(); } Dimension2D compBounds = component.getPreferredSize(); Constraints constraints = getConstraints(component, container); double componentWidth; double componentX; if (constraints.isStretched()) { componentWidth = width; componentX = xMin; } else { componentWidth = Math.min(compBounds.getWidth(), width); componentX = xMin + (width - componentWidth)*constraints.getAlignmentX(); } component.setBounds(componentX, yMin, componentWidth, compBounds.getHeight()); yMin += compBounds.getHeight(); } } } StackedLayout(Orientation orientation);  StackedLayout(Orientation orientation, double gapX, double gapY); void layout(Container container); Dimension2D getPreferredSize(Container container);  }
@Test public void testLayoutVertical() { Layout layout = new StackedLayout(Orientation.VERTICAL, GAP_X, GAP_Y); Rectangle2D bounds = new Rectangle2D.Double(5.0, 5.0, 50.0, 50.0); container.setBounds(bounds); layout.layout(container); assertEquals(bounds.getMinX(), a.getX(), DELTA); assertEquals(bounds.getMinX(), b.getX(), DELTA); assertEquals(bounds.getMinX(), c.getX(), DELTA); assertEquals(12.5, a.getY(), DELTA); assertEquals(27.5, b.getY(), DELTA); assertEquals(42.5, c.getY(), DELTA); }
@Test public void testLayoutHorizontal() { Layout layout = new StackedLayout(Orientation.HORIZONTAL, GAP_X, GAP_Y); Rectangle2D bounds = new Rectangle2D.Double(5.0, 5.0, 50.0, 50.0); container.setBounds(bounds); layout.layout(container); assertEquals(10.0, a.getX(), DELTA); assertEquals(25.0, b.getX(), DELTA); assertEquals(40.0, c.getX(), DELTA); assertEquals(bounds.getMinY(), a.getY(), DELTA); assertEquals(bounds.getMinY(), b.getY(), DELTA); assertEquals(bounds.getMinY(), c.getY(), DELTA); }