1 7 package java.awt; 8 9 import java.io.ObjectInputStream ; 10 import java.io.IOException ; 11 12 68 public class FlowLayout implements LayoutManager , java.io.Serializable { 69 70 74 public static final int LEFT = 0; 75 76 80 public static final int CENTER = 1; 81 82 86 public static final int RIGHT = 2; 87 88 98 public static final int LEADING = 3; 99 100 110 public static final int TRAILING = 4; 111 112 128 int align; 130 148 int newAlign; 150 161 int hgap; 162 163 173 int vgap; 174 175 178 private static final long serialVersionUID = -7262534875583282631L; 179 180 184 public FlowLayout() { 185 this(CENTER, 5, 5); 186 } 187 188 197 public FlowLayout(int align) { 198 this(align, 5, 5); 199 } 200 201 217 public FlowLayout(int align, int hgap, int vgap) { 218 this.hgap = hgap; 219 this.vgap = vgap; 220 setAlignment(align); 221 } 222 223 233 public int getAlignment() { 234 return newAlign; 235 } 236 237 251 public void setAlignment(int align) { 252 this.newAlign = align; 253 254 258 switch (align) { 259 case LEADING: 260 this.align = LEFT; 261 break; 262 case TRAILING: 263 this.align = RIGHT; 264 break; 265 default: 266 this.align = align; 267 break; 268 } 269 } 270 271 282 public int getHgap() { 283 return hgap; 284 } 285 286 297 public void setHgap(int hgap) { 298 this.hgap = hgap; 299 } 300 301 312 public int getVgap() { 313 return vgap; 314 } 315 316 326 public void setVgap(int vgap) { 327 this.vgap = vgap; 328 } 329 330 336 public void addLayoutComponent(String name, Component comp) { 337 } 338 339 345 public void removeLayoutComponent(Component comp) { 346 } 347 348 359 public Dimension preferredLayoutSize(Container target) { 360 synchronized (target.getTreeLock()) { 361 Dimension dim = new Dimension (0, 0); 362 int nmembers = target.getComponentCount(); 363 boolean firstVisibleComponent = true; 364 365 for (int i = 0 ; i < nmembers ; i++) { 366 Component m = target.getComponent(i); 367 if (m.visible) { 368 Dimension d = m.getPreferredSize(); 369 dim.height = Math.max(dim.height, d.height); 370 if (firstVisibleComponent) { 371 firstVisibleComponent = false; 372 } else { 373 dim.width += hgap; 374 } 375 dim.width += d.width; 376 } 377 } 378 Insets insets = target.getInsets(); 379 dim.width += insets.left + insets.right + hgap*2; 380 dim.height += insets.top + insets.bottom + vgap*2; 381 return dim; 382 } 383 } 384 385 395 public Dimension minimumLayoutSize(Container target) { 396 synchronized (target.getTreeLock()) { 397 Dimension dim = new Dimension (0, 0); 398 int nmembers = target.getComponentCount(); 399 400 for (int i = 0 ; i < nmembers ; i++) { 401 Component m = target.getComponent(i); 402 if (m.visible) { 403 Dimension d = m.getMinimumSize(); 404 dim.height = Math.max(dim.height, d.height); 405 if (i > 0) { 406 dim.width += hgap; 407 } 408 dim.width += d.width; 409 } 410 } 411 Insets insets = target.getInsets(); 412 dim.width += insets.left + insets.right + hgap*2; 413 dim.height += insets.top + insets.bottom + vgap*2; 414 return dim; 415 } 416 } 417 418 428 private void moveComponents(Container target, int x, int y, int width, int height, 429 int rowStart, int rowEnd, boolean ltr) { 430 synchronized (target.getTreeLock()) { 431 switch (newAlign) { 432 case LEFT: 433 x += ltr ? 0 : width; 434 break; 435 case CENTER: 436 x += width / 2; 437 break; 438 case RIGHT: 439 x += ltr ? width : 0; 440 break; 441 case LEADING: 442 break; 443 case TRAILING: 444 x += width; 445 break; 446 } 447 for (int i = rowStart ; i < rowEnd ; i++) { 448 Component m = target.getComponent(i); 449 if (m.visible) { 450 if (ltr) { 451 m.setLocation(x, y + (height - m.height) / 2); 452 } else { 453 m.setLocation(target.width - x - m.width, y + (height - m.height) / 2); 454 } 455 x += m.width + hgap; 456 } 457 } 458 } 459 } 460 461 472 public void layoutContainer(Container target) { 473 synchronized (target.getTreeLock()) { 474 Insets insets = target.getInsets(); 475 int maxwidth = target.width - (insets.left + insets.right + hgap*2); 476 int nmembers = target.getComponentCount(); 477 int x = 0, y = insets.top + vgap; 478 int rowh = 0, start = 0; 479 480 boolean ltr = target.getComponentOrientation().isLeftToRight(); 481 482 for (int i = 0 ; i < nmembers ; i++) { 483 Component m = target.getComponent(i); 484 if (m.visible) { 485 Dimension d = m.getPreferredSize(); 486 m.setSize(d.width, d.height); 487 488 if ((x == 0) || ((x + d.width) <= maxwidth)) { 489 if (x > 0) { 490 x += hgap; 491 } 492 x += d.width; 493 rowh = Math.max(rowh, d.height); 494 } else { 495 moveComponents(target, insets.left + hgap, y, maxwidth - x, rowh, start, i, ltr); 496 x = d.width; 497 y += vgap + rowh; 498 rowh = d.height; 499 start = i; 500 } 501 } 502 } 503 moveComponents(target, insets.left + hgap, y, maxwidth - x, rowh, start, nmembers, ltr); 504 } 505 } 506 507 private static final int currentSerialVersion = 1; 513 522 private int serialVersionOnStream = currentSerialVersion; 523 524 529 private void readObject(ObjectInputStream stream) 530 throws IOException , ClassNotFoundException 531 { 532 stream.defaultReadObject(); 533 534 if (serialVersionOnStream < 1) { 535 setAlignment(this.align); 537 } 538 serialVersionOnStream = currentSerialVersion; 539 } 540 541 546 public String toString() { 547 String str = ""; 548 switch (align) { 549 case LEFT: str = ",align=left"; break; 550 case CENTER: str = ",align=center"; break; 551 case RIGHT: str = ",align=right"; break; 552 case LEADING: str = ",align=leading"; break; 553 case TRAILING: str = ",align=trailing"; break; 554 } 555 return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + str + "]"; 556 } 557 558 559 } 560 | Popular Tags |