1 7 8 package java.awt; 9 10 76 public class GridLayout implements LayoutManager , java.io.Serializable { 77 86 int hgap; 87 96 int vgap; 97 108 int rows; 109 120 int cols; 121 122 127 public GridLayout() { 128 this(1, 0, 0, 0); 129 } 130 131 143 public GridLayout(int rows, int cols) { 144 this(rows, cols, 0, 0); 145 } 146 147 171 public GridLayout(int rows, int cols, int hgap, int vgap) { 172 if ((rows == 0) && (cols == 0)) { 173 throw new IllegalArgumentException ("rows and cols cannot both be zero"); 174 } 175 this.rows = rows; 176 this.cols = cols; 177 this.hgap = hgap; 178 this.vgap = vgap; 179 } 180 181 186 public int getRows() { 187 return rows; 188 } 189 190 197 public void setRows(int rows) { 198 if ((rows == 0) && (this.cols == 0)) { 199 throw new IllegalArgumentException ("rows and cols cannot both be zero"); 200 } 201 this.rows = rows; 202 } 203 204 209 public int getColumns() { 210 return cols; 211 } 212 213 225 public void setColumns(int cols) { 226 if ((cols == 0) && (this.rows == 0)) { 227 throw new IllegalArgumentException ("rows and cols cannot both be zero"); 228 } 229 this.cols = cols; 230 } 231 232 237 public int getHgap() { 238 return hgap; 239 } 240 241 246 public void setHgap(int hgap) { 247 this.hgap = hgap; 248 } 249 250 255 public int getVgap() { 256 return vgap; 257 } 258 259 264 public void setVgap(int vgap) { 265 this.vgap = vgap; 266 } 267 268 273 public void addLayoutComponent(String name, Component comp) { 274 } 275 276 280 public void removeLayoutComponent(Component comp) { 281 } 282 283 303 public Dimension preferredLayoutSize(Container parent) { 304 synchronized (parent.getTreeLock()) { 305 Insets insets = parent.getInsets(); 306 int ncomponents = parent.getComponentCount(); 307 int nrows = rows; 308 int ncols = cols; 309 310 if (nrows > 0) { 311 ncols = (ncomponents + nrows - 1) / nrows; 312 } else { 313 nrows = (ncomponents + ncols - 1) / ncols; 314 } 315 int w = 0; 316 int h = 0; 317 for (int i = 0 ; i < ncomponents ; i++) { 318 Component comp = parent.getComponent(i); 319 Dimension d = comp.getPreferredSize(); 320 if (w < d.width) { 321 w = d.width; 322 } 323 if (h < d.height) { 324 h = d.height; 325 } 326 } 327 return new Dimension (insets.left + insets.right + ncols*w + (ncols-1)*hgap, 328 insets.top + insets.bottom + nrows*h + (nrows-1)*vgap); 329 } 330 } 331 332 352 public Dimension minimumLayoutSize(Container parent) { 353 synchronized (parent.getTreeLock()) { 354 Insets insets = parent.getInsets(); 355 int ncomponents = parent.getComponentCount(); 356 int nrows = rows; 357 int ncols = cols; 358 359 if (nrows > 0) { 360 ncols = (ncomponents + nrows - 1) / nrows; 361 } else { 362 nrows = (ncomponents + ncols - 1) / ncols; 363 } 364 int w = 0; 365 int h = 0; 366 for (int i = 0 ; i < ncomponents ; i++) { 367 Component comp = parent.getComponent(i); 368 Dimension d = comp.getMinimumSize(); 369 if (w < d.width) { 370 w = d.width; 371 } 372 if (h < d.height) { 373 h = d.height; 374 } 375 } 376 return new Dimension (insets.left + insets.right + ncols*w + (ncols-1)*hgap, 377 insets.top + insets.bottom + nrows*h + (nrows-1)*vgap); 378 } 379 } 380 381 399 public void layoutContainer(Container parent) { 400 synchronized (parent.getTreeLock()) { 401 Insets insets = parent.getInsets(); 402 int ncomponents = parent.getComponentCount(); 403 int nrows = rows; 404 int ncols = cols; 405 boolean ltr = parent.getComponentOrientation().isLeftToRight(); 406 407 if (ncomponents == 0) { 408 return; 409 } 410 if (nrows > 0) { 411 ncols = (ncomponents + nrows - 1) / nrows; 412 } else { 413 nrows = (ncomponents + ncols - 1) / ncols; 414 } 415 int w = parent.width - (insets.left + insets.right); 416 int h = parent.height - (insets.top + insets.bottom); 417 w = (w - (ncols - 1) * hgap) / ncols; 418 h = (h - (nrows - 1) * vgap) / nrows; 419 420 if (ltr) { 421 for (int c = 0, x = insets.left ; c < ncols ; c++, x += w + hgap) { 422 for (int r = 0, y = insets.top ; r < nrows ; r++, y += h + vgap) { 423 int i = r * ncols + c; 424 if (i < ncomponents) { 425 parent.getComponent(i).setBounds(x, y, w, h); 426 } 427 } 428 } 429 } else { 430 for (int c = 0, x = parent.width - insets.right - w; c < ncols ; c++, x -= w + hgap) { 431 for (int r = 0, y = insets.top ; r < nrows ; r++, y += h + vgap) { 432 int i = r * ncols + c; 433 if (i < ncomponents) { 434 parent.getComponent(i).setBounds(x, y, w, h); 435 } 436 } 437 } 438 } 439 } 440 } 441 442 446 public String toString() { 447 return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + 448 ",rows=" + rows + ",cols=" + cols + "]"; 449 } 450 } 451 | Popular Tags |