1 30 31 package rcm.awt; 32 33 import java.awt.*; 34 35 public class BorderPanel extends Panel { 36 37 int left, top, bottom, right; 38 39 public BorderPanel (Insets insets) { 40 this.left = insets.left; 41 this.top = insets.top; 42 this.bottom = insets.bottom; 43 this.right = insets.right; 44 } 45 46 public BorderPanel (int left, int top, int bottom, int right) { 47 this.left = left; 48 this.top = top; 49 this.bottom = bottom; 50 this.right = right; 51 } 52 53 54 public void layout () { 55 Dimension d = getSize (); 56 57 int x = left; 58 int y = top; 59 int w = d.width - left - right; 60 int h = d.height - top - bottom; 61 62 Component[] comps = getComponents (); 63 for (int i=0; i<comps.length; ++i) 64 comps[i].setBounds (x, y, w, h); 65 } 66 67 public Dimension getPreferredSize () { 68 Dimension max = new Dimension (0, 0); 69 70 Component[] comps = getComponents (); 71 for (int i=0; i<comps.length; ++i) { 72 Dimension d = comps[i].getPreferredSize (); 73 max.width = Math.max (d.width, max.width); 74 max.height = Math.max (d.height, max.height); 75 } 76 77 max.width += left+right; 78 max.height += top+bottom; 79 return max; 80 } 81 82 public Dimension getMinimumSize () { 83 Dimension max = new Dimension (0, 0); 84 85 Component[] comps = getComponents (); 86 for (int i=0; i<comps.length; ++i) { 87 Dimension d = comps[i].getMinimumSize (); 88 max.width = Math.max (d.width, max.width); 89 max.height = Math.max (d.height, max.height); 90 } 91 92 max.width += left+right; 93 max.height += top+bottom; 94 return max; 95 } 96 97 public static Panel wrap (Component comp, Insets insets) { 98 Panel p = new BorderPanel (insets); 99 p.add (comp); 100 return p; 101 } 102 103 public static Panel wrap (Component comp, int left, int top, int bottom, int right) { 104 Panel p = new BorderPanel (left, top, bottom, right); 105 p.add (comp); 106 return p; 107 } 108 109 public static Panel wrap (Component comp, int horiz, int vert) { 110 Panel p = new BorderPanel (horiz, vert, horiz, vert); 111 p.add (comp); 112 return p; 113 } 114 115 public static Panel wrap (Component comp, int all) { 116 Panel p = new BorderPanel (all, all, all, all); 117 p.add (comp); 118 return p; 119 } 120 } 121 | Popular Tags |