@Test public void testCenterAlignment() { EnhancedFlowLayout flow = new EnhancedFlowLayout(EnhancedFlowLayout.LEFT, EnhancedFlowLayout.CENTER); container.setLayout(flow); container.add(one); container.add(two); flow.layoutContainer(container); assertEquals(one.getWidth(), 20); assertEquals(one.getHeight(), 10); assertEquals(one.getX(), 0); assertEquals(getYCenter(one), container.getSize().getHeight() / 2.0, 0.5); assertEquals(two.getWidth(), 15); assertEquals(two.getHeight(), 5); assertEquals(two.getX(), 20); assertEquals(getYCenter(two), container.getSize().getHeight() / 2.0, 0.5); }
@Override public void layoutContainer(Container target) { synchronized (target.getTreeLock()) { super.layoutContainer(target); if (verticalAlignment != TOP) { int minY=Integer.MAX_VALUE; int maxY=0; int nMembers = target.getComponentCount(); for (int i=0; i<nMembers; ++i) { Component comp = target.getComponent(i); int compMinY = comp.getLocation().y; int compMaxY = compMinY + comp.getHeight(); maxY = Math.max(compMaxY, maxY); minY = Math.min(compMinY, minY); } Insets insets = target.getInsets(); int availableHeight = target.getHeight() - (insets.bottom + insets.top + getVgap()*2); int delta = availableHeight - (maxY - minY); if (verticalAlignment == CENTER) delta /= 2; for (int i=0; i<nMembers; ++i) { Component comp = target.getComponent(i); Point newLoc = comp.getLocation(); newLoc.y += delta; comp.setLocation(newLoc); } } } }
EnhancedFlowLayout extends FlowLayout { @Override public void layoutContainer(Container target) { synchronized (target.getTreeLock()) { super.layoutContainer(target); if (verticalAlignment != TOP) { int minY=Integer.MAX_VALUE; int maxY=0; int nMembers = target.getComponentCount(); for (int i=0; i<nMembers; ++i) { Component comp = target.getComponent(i); int compMinY = comp.getLocation().y; int compMaxY = compMinY + comp.getHeight(); maxY = Math.max(compMaxY, maxY); minY = Math.min(compMinY, minY); } Insets insets = target.getInsets(); int availableHeight = target.getHeight() - (insets.bottom + insets.top + getVgap()*2); int delta = availableHeight - (maxY - minY); if (verticalAlignment == CENTER) delta /= 2; for (int i=0; i<nMembers; ++i) { Component comp = target.getComponent(i); Point newLoc = comp.getLocation(); newLoc.y += delta; comp.setLocation(newLoc); } } } } }
EnhancedFlowLayout extends FlowLayout { @Override public void layoutContainer(Container target) { synchronized (target.getTreeLock()) { super.layoutContainer(target); if (verticalAlignment != TOP) { int minY=Integer.MAX_VALUE; int maxY=0; int nMembers = target.getComponentCount(); for (int i=0; i<nMembers; ++i) { Component comp = target.getComponent(i); int compMinY = comp.getLocation().y; int compMaxY = compMinY + comp.getHeight(); maxY = Math.max(compMaxY, maxY); minY = Math.min(compMinY, minY); } Insets insets = target.getInsets(); int availableHeight = target.getHeight() - (insets.bottom + insets.top + getVgap()*2); int delta = availableHeight - (maxY - minY); if (verticalAlignment == CENTER) delta /= 2; for (int i=0; i<nMembers; ++i) { Component comp = target.getComponent(i); Point newLoc = comp.getLocation(); newLoc.y += delta; comp.setLocation(newLoc); } } } } EnhancedFlowLayout(int horizontalAlignment, int verticalAlignment);  EnhancedFlowLayout(int horizontalAlignment, int verticalAlignment, int hGap, int vGap); }
EnhancedFlowLayout extends FlowLayout { @Override public void layoutContainer(Container target) { synchronized (target.getTreeLock()) { super.layoutContainer(target); if (verticalAlignment != TOP) { int minY=Integer.MAX_VALUE; int maxY=0; int nMembers = target.getComponentCount(); for (int i=0; i<nMembers; ++i) { Component comp = target.getComponent(i); int compMinY = comp.getLocation().y; int compMaxY = compMinY + comp.getHeight(); maxY = Math.max(compMaxY, maxY); minY = Math.min(compMinY, minY); } Insets insets = target.getInsets(); int availableHeight = target.getHeight() - (insets.bottom + insets.top + getVgap()*2); int delta = availableHeight - (maxY - minY); if (verticalAlignment == CENTER) delta /= 2; for (int i=0; i<nMembers; ++i) { Component comp = target.getComponent(i); Point newLoc = comp.getLocation(); newLoc.y += delta; comp.setLocation(newLoc); } } } } EnhancedFlowLayout(int horizontalAlignment, int verticalAlignment);  EnhancedFlowLayout(int horizontalAlignment, int verticalAlignment, int hGap, int vGap); int getVerticalAlignment(); void setVerticalAlignment(int verticalAlignment); @Override void layoutContainer(Container target); }
EnhancedFlowLayout extends FlowLayout { @Override public void layoutContainer(Container target) { synchronized (target.getTreeLock()) { super.layoutContainer(target); if (verticalAlignment != TOP) { int minY=Integer.MAX_VALUE; int maxY=0; int nMembers = target.getComponentCount(); for (int i=0; i<nMembers; ++i) { Component comp = target.getComponent(i); int compMinY = comp.getLocation().y; int compMaxY = compMinY + comp.getHeight(); maxY = Math.max(compMaxY, maxY); minY = Math.min(compMinY, minY); } Insets insets = target.getInsets(); int availableHeight = target.getHeight() - (insets.bottom + insets.top + getVgap()*2); int delta = availableHeight - (maxY - minY); if (verticalAlignment == CENTER) delta /= 2; for (int i=0; i<nMembers; ++i) { Component comp = target.getComponent(i); Point newLoc = comp.getLocation(); newLoc.y += delta; comp.setLocation(newLoc); } } } } EnhancedFlowLayout(int horizontalAlignment, int verticalAlignment);  EnhancedFlowLayout(int horizontalAlignment, int verticalAlignment, int hGap, int vGap); int getVerticalAlignment(); void setVerticalAlignment(int verticalAlignment); @Override void layoutContainer(Container target); final static long serialVersionUID; final static int TOP; final static int BOTTOM; }