1 import javax.swing.*; 2 import javax.swing.SpringLayout ; 3 import java.awt.*; 4 5 11 public class SpringUtilities { 12 16 public static void printSizes(Component c) { 17 System.out.println("minimumSize = " + c.getMinimumSize()); 18 System.out.println("preferredSize = " + c.getPreferredSize()); 19 System.out.println("maximumSize = " + c.getMaximumSize()); 20 } 21 22 36 public static void makeGrid(Container parent, 37 int rows, int cols, 38 int initialX, int initialY, 39 int xPad, int yPad) { 40 SpringLayout layout; 41 try { 42 layout = (SpringLayout )parent.getLayout(); 43 } catch (ClassCastException exc) { 44 System.err.println("The first argument to makeGrid must use SpringLayout."); 45 return; 46 } 47 48 Spring xPadSpring = Spring.constant(xPad); 49 Spring yPadSpring = Spring.constant(yPad); 50 Spring initialXSpring = Spring.constant(initialX); 51 Spring initialYSpring = Spring.constant(initialY); 52 int max = rows * cols; 53 54 Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)). 57 getWidth(); 58 Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)). 59 getWidth(); 60 for (int i = 1; i < max; i++) { 61 SpringLayout.Constraints cons = layout.getConstraints( 62 parent.getComponent(i)); 63 64 maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth()); 65 maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight()); 66 } 67 68 for (int i = 0; i < max; i++) { 71 SpringLayout.Constraints cons = layout.getConstraints( 72 parent.getComponent(i)); 73 74 cons.setWidth(maxWidthSpring); 75 cons.setHeight(maxHeightSpring); 76 } 77 78 SpringLayout.Constraints lastCons = null; 81 SpringLayout.Constraints lastRowCons = null; 82 for (int i = 0; i < max; i++) { 83 SpringLayout.Constraints cons = layout.getConstraints( 84 parent.getComponent(i)); 85 if (i % cols == 0) { lastRowCons = lastCons; 87 cons.setX(initialXSpring); 88 } else { cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST), 90 xPadSpring)); 91 } 92 93 if (i / cols == 0) { cons.setY(initialYSpring); 95 } else { cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH), 97 yPadSpring)); 98 } 99 lastCons = cons; 100 } 101 102 SpringLayout.Constraints pCons = layout.getConstraints(parent); 104 pCons.setConstraint(SpringLayout.SOUTH, 105 Spring.sum( 106 Spring.constant(yPad), 107 lastCons.getConstraint(SpringLayout.SOUTH))); 108 pCons.setConstraint(SpringLayout.EAST, 109 Spring.sum( 110 Spring.constant(xPad), 111 lastCons.getConstraint(SpringLayout.EAST))); 112 } 113 114 115 private static SpringLayout.Constraints getConstraintsForCell( 116 int row, int col, 117 Container parent, 118 int cols) { 119 SpringLayout layout = (SpringLayout ) parent.getLayout(); 120 Component c = parent.getComponent(row * cols + col); 121 return layout.getConstraints(c); 122 } 123 124 139 public static void makeCompactGrid(Container parent, 140 int rows, int cols, 141 int initialX, int initialY, 142 int xPad, int yPad) { 143 SpringLayout layout; 144 try { 145 layout = (SpringLayout )parent.getLayout(); 146 } catch (ClassCastException exc) { 147 System.err.println("The first argument to makeCompactGrid must use SpringLayout."); 148 return; 149 } 150 151 Spring x = Spring.constant(initialX); 153 for (int c = 0; c < cols; c++) { 154 Spring width = Spring.constant(0); 155 for (int r = 0; r < rows; r++) { 156 width = Spring.max(width, 157 getConstraintsForCell(r, c, parent, cols). 158 getWidth()); 159 } 160 for (int r = 0; r < rows; r++) { 161 SpringLayout.Constraints constraints = 162 getConstraintsForCell(r, c, parent, cols); 163 constraints.setX(x); 164 constraints.setWidth(width); 165 } 166 x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad))); 167 } 168 169 Spring y = Spring.constant(initialY); 171 for (int r = 0; r < rows; r++) { 172 Spring height = Spring.constant(0); 173 for (int c = 0; c < cols; c++) { 174 height = Spring.max(height, 175 getConstraintsForCell(r, c, parent, cols). 176 getHeight()); 177 } 178 for (int c = 0; c < cols; c++) { 179 SpringLayout.Constraints constraints = 180 getConstraintsForCell(r, c, parent, cols); 181 constraints.setY(y); 182 constraints.setHeight(height); 183 } 184 y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad))); 185 } 186 187 SpringLayout.Constraints pCons = layout.getConstraints(parent); 189 pCons.setConstraint(SpringLayout.SOUTH, y); 190 pCons.setConstraint(SpringLayout.EAST, x); 191 } 192 } 193 | Popular Tags |