1 7 8 package javax.swing.colorchooser; 9 10 import java.awt.*; 11 import java.io.*; 12 13 14 20 class CenterLayout implements LayoutManager, Serializable { 21 public void addLayoutComponent(String name, Component comp) { } 22 public void removeLayoutComponent(Component comp) { } 23 24 public Dimension preferredLayoutSize( Container container ) { 25 Component c = container.getComponent( 0 ); 26 if ( c != null ) { 27 Dimension size = c.getPreferredSize(); 28 Insets insets = container.getInsets(); 29 size.width += insets.left + insets.right; 30 size.height += insets.top + insets.bottom; 31 return size; 32 } 33 else { 34 return new Dimension( 0, 0 ); 35 } 36 } 37 38 public Dimension minimumLayoutSize(Container cont) { 39 return preferredLayoutSize(cont); 40 } 41 42 public void layoutContainer(Container container) { 43 try { 44 Component c = container.getComponent( 0 ); 45 46 c.setSize( c.getPreferredSize() ); 47 Dimension size = c.getSize(); 48 Dimension containerSize = container.getSize(); 49 Insets containerInsets = container.getInsets(); 50 containerSize.width -= containerInsets.left + containerInsets.right; 51 containerSize.height -= containerInsets.top + containerInsets.bottom; 52 int componentLeft = (containerSize.width / 2) - (size.width / 2); 53 int componentTop = (containerSize.height / 2) - (size.height / 2); 54 componentLeft += containerInsets.left; 55 componentTop += containerInsets.top; 56 57 c.setBounds( componentLeft, componentTop, size.width, size.height ); 58 } 59 catch( Exception e ) { 60 } 61 } 62 } 63 | Popular Tags |