package NET.worlds.console; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.Insets; import java.awt.LayoutManager; class StackedLayout implements LayoutManager { public StackedLayout() { } @Override public void addLayoutComponent(String name, Component c) { } @Override public void removeLayoutComponent(Component c) { } @Override public Dimension preferredLayoutSize(Container target) { Dimension size = new Dimension(0, 0); int count = target.getComponentCount(); for (int i = 0; i < count; i++) { Component m = target.getComponent(i); Dimension d = m.getPreferredSize(); size.width = Math.max(size.width, d.width); size.height = size.height + d.height; } Insets insets = target.getInsets(); size.width = size.width + insets.left + insets.right; size.height = size.height + insets.top + insets.bottom; return size; } @Override public Dimension minimumLayoutSize(Container target) { return this.preferredLayoutSize(target); } @Override public void layoutContainer(Container target) { Insets insets = target.getInsets(); Dimension size = target.getSize(); int x = insets.left; int y = insets.top; int width = size.width - insets.left - insets.right; int height = size.height - insets.top - insets.bottom; int count = target.getComponentCount(); for (int i = 0; i < count; i++) { Component c = target.getComponent(i); int h = c.getPreferredSize().height; if (i == count - 1) { h = Math.max(h, height); } else { h = Math.min(h, height); } h = Math.max(0, h); c.setBounds(x, y, width, h); height -= h; y += h; } } }