This LayoutManager arranges the components into a column. Components are always given their preferred size : 自定义布局 « Swing « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » Swing » 自定义布局 
14. 97. 4. This LayoutManager arranges the components into a column. Components are always given their preferred size
/*
 * Copyright (c) 2004 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose,
 * including teaching and use in open-source projects.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book, 
 * please visit http://www.davidflanagan.com/javaexamples3.
 */

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.LayoutManager2;

import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * This LayoutManager arranges the components into a column. Components are
 * always given their preferred size.
 
 * When you create a ColumnLayout, you may specify four values: margin_height --
 * how much space to leave on top and bottom margin_width -- how much space to
 * leave on left and right spacing -- how much vertical space to leave between
 * items alignment -- the horizontal position of the components:
 * ColumnLayout.LEFT -- left-justify the components ColumnLayout.CENTER --
 * horizontally center the components ColumnLayout.RIGHT -- right-justify the
 * components
 
 * You never call the methods of a ColumnLayout object. Just create one and make
 * it the layout manager for your container by passing it to the addLayout()
 * method of the Container object.
 */
public class ColumnLayout implements LayoutManager2 {
  protected int margin_height;

  protected int margin_width;

  protected int spacing;

  protected int alignment;

  // Constants for the alignment argument to the constructor.
  public static final int LEFT = 0;

  public static final int CENTER = 1;

  public static final int RIGHT = 2;

  /** The constructor. See comment above for meanings of these arguments */
  public ColumnLayout(int margin_height, int margin_width, int spacing, int alignment) {
    this.margin_height = margin_height;
    this.margin_width = margin_width;
    this.spacing = spacing;
    this.alignment = alignment;
  }

  /**
   * A default constructor that creates a ColumnLayout using 5-pixel margin
   * width and height, 5-pixel spacing, and left alignment
   */
  public ColumnLayout() {
    this(555, LEFT);
  }

  /**
   * The method that actually performs the layout. Called by the Container
   */
  public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    Dimension parent_size = parent.getSize();
    Component kid;
    int nkids = parent.getComponentCount();
    int x0 = insets.left + margin_width; // The base X position
    int x;
    int y = insets.top + margin_height; // Start at the top of the column

    for (int i = 0; i < nkids; i++) { // Loop through the kids
      kid = parent.getComponent(i)// Get the kid
      if (!kid.isVisible())
        continue// Skip hidden ones
      Dimension pref = kid.getPreferredSize()// How big is it?
      switch (alignment) { // Compute X coordinate
      default:
      case LEFT:
        x = x0;
        break;
      case CENTER:
        x = (parent_size.width - pref.width2;
        break;
      case RIGHT:
        x = parent_size.width - insets.right - margin_width - pref.width;
        break;
      }
      // Set the size and position of this kid
      kid.setBounds(x, y, pref.width, pref.height);
      y += pref.height + spacing; // Get Y position of the next one
    }
  }

  /** The Container calls this to find out how big the layout should to be */
  public Dimension preferredLayoutSize(Container parent) {
    return layoutSize(parent, 1);
  }

  /** The Container calls this to find out how big the layout must be */
  public Dimension minimumLayoutSize(Container parent) {
    return layoutSize(parent, 2);
  }

  /** The Container calls this to find out how big the layout can be */
  public Dimension maximumLayoutSize(Container parent) {
    return layoutSize(parent, 3);
  }

  // Compute min, max, or preferred size of all the visible children
  protected Dimension layoutSize(Container parent, int sizetype) {
    int nkids = parent.getComponentCount();
    Dimension size = new Dimension(00);
    Insets insets = parent.getInsets();
    int num_visible_kids = 0;

    // Compute maximum width and total height of all visible kids
    for (int i = 0; i < nkids; i++) {
      Component kid = parent.getComponent(i);
      Dimension d;
      if (!kid.isVisible())
        continue;
      num_visible_kids++;
      if (sizetype == 1)
        d = kid.getPreferredSize();
      else if (sizetype == 2)
        d = kid.getMinimumSize();
      else
        d = kid.getMaximumSize();
      if (d.width > size.width)
        size.width = d.width;
      size.height += d.height;
    }

    // Now add in margins and stuff
    size.width += insets.left + insets.right + * margin_width;
    size.height += insets.top + insets.bottom + * margin_height;
    if (num_visible_kids > 1)
      size.height += (num_visible_kids - 1* spacing;
    return size;
  }

  // Other LayoutManager(2) methods that are unused by this class
  public void addLayoutComponent(String constraint, Component comp) {
  }

  public void addLayoutComponent(Component comp, Object constraint) {
  }

  public void removeLayoutComponent(Component comp) {
  }

  public void invalidateLayout(Container parent) {
  }

  public float getLayoutAlignmentX(Container parent) {
    return 0.5f;
  }

  public float getLayoutAlignmentY(Container parent) {
    return 0.5f;
  }
}

/*
 * Copyright (c) 2004 David Flanagan. All rights reserved. This code is from the
 * book Java Examples in a Nutshell, 3nd Edition. It is provided AS-IS, WITHOUT
 * ANY WARRANTY either expressed or implied. You may study, use, and modify it
 * for any non-commercial purpose, including teaching and use in open-source
 * projects. You may distribute it non-commercially as long as you retain this
 * notice. For a commercial use license, or to purchase the book, please visit
 * http://www.davidflanagan.com/javaexamples3.
 */
class ColumnLayoutPane extends JPanel {
  public ColumnLayoutPane() {
    // Specify a ColumnLayout LayoutManager, with right alignment
    this.setLayout(new ColumnLayout(5510, ColumnLayout.RIGHT));

    // Create some buttons and set their sizes and positions explicitly
    for (int i = 0; i < 6; i++) {
      int pointsize = + i * 2;
      JButton b = new JButton("Point size " + pointsize);
      b.setFont(new Font("helvetica", Font.BOLD, pointsize));
      this.add(b);
    }
  }
}
14. 97. 自定义布局
14. 97. 1. 自定义布局: DiagonalLayout自定义布局: DiagonalLayout
14. 97. 2. GraphPaperLayout implements LayoutManager2
14. 97. 3. 圆布局
14. 97. 4. This LayoutManager arranges the components into a column. Components are always given their preferred size
14. 97. 5. A layout manager that lays out components along a central axis
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.