예제 #1
0
  /**
   * Lays out the specified container using this layout.
   *
   * <p>This method reshapes the components in the specified target container in order to satisfy
   * the constraints of the <code>GridLayout</code> object.
   *
   * <p>The grid layout manager determines the size of individual components by dividing the free
   * space in the container into equal-sized portions according to the number of rows and columns in
   * the layout. The container's free space equals the container's size minus any insets and any
   * specified horizontal or vertical gap. All components in a grid layout are given the same size.
   *
   * @param parent the container in which to do the layout
   * @see java.awt.Container
   * @see java.awt.Container#doLayout
   */
  public void layoutContainer(Container parent) {
    synchronized (parent.getTreeLock()) {
      Insets insets = parent.getInsets();
      int ncomponents = parent.getComponentCount();
      int nrows = rows;
      int ncols = cols;
      boolean ltr = parent.getComponentOrientation().isLeftToRight();

      if (ncomponents == 0) {
        return;
      }
      if (nrows > 0) {
        ncols = (ncomponents + nrows - 1) / nrows;
      } else {
        nrows = (ncomponents + ncols - 1) / ncols;
      }
      // 4370316. To position components in the center we should:
      // 1. get an amount of extra space within Container
      // 2. incorporate half of that value to the left/top position
      // Note that we use trancating division for widthOnComponent
      // The reminder goes to extraWidthAvailable
      int totalGapsWidth = (ncols - 1) * hgap;
      int widthWOInsets = parent.width - (insets.left + insets.right);
      int widthOnComponent = (widthWOInsets - totalGapsWidth) / ncols;
      int extraWidthAvailable = (widthWOInsets - (widthOnComponent * ncols + totalGapsWidth)) / 2;

      int totalGapsHeight = (nrows - 1) * vgap;
      int heightWOInsets = parent.height - (insets.top + insets.bottom);
      int heightOnComponent = (heightWOInsets - totalGapsHeight) / nrows;
      int extraHeightAvailable =
          (heightWOInsets - (heightOnComponent * nrows + totalGapsHeight)) / 2;
      if (ltr) {
        for (int c = 0, x = insets.left + extraWidthAvailable;
            c < ncols;
            c++, x += widthOnComponent + hgap) {
          for (int r = 0, y = insets.top + extraHeightAvailable;
              r < nrows;
              r++, y += heightOnComponent + vgap) {
            int i = r * ncols + c;
            if (i < ncomponents) {
              parent.getComponent(i).setBounds(x, y, widthOnComponent, heightOnComponent);
            }
          }
        }
      } else {
        for (int c = 0, x = (parent.width - insets.right - widthOnComponent) - extraWidthAvailable;
            c < ncols;
            c++, x -= widthOnComponent + hgap) {
          for (int r = 0, y = insets.top + extraHeightAvailable;
              r < nrows;
              r++, y += heightOnComponent + vgap) {
            int i = r * ncols + c;
            if (i < ncomponents) {
              parent.getComponent(i).setBounds(x, y, widthOnComponent, heightOnComponent);
            }
          }
        }
      }
    }
  }
예제 #2
0
  /**
   * Lays out the specified container using this layout.
   *
   * <p>This method reshapes the components in the specified target container in order to satisfy
   * the constraints of the <code>GridLayout</code> object.
   *
   * <p>The grid layout manager determines the size of individual components by dividing the free
   * space in the container into equal-sized portions according to the number of rows and columns in
   * the layout. The container's free space equals the container's size minus any insets and any
   * specified horizontal or vertical gap. All components in a grid layout are given the same size.
   *
   * @param parent the container in which to do the layout
   * @see java.awt.Container
   * @see java.awt.Container#doLayout
   */
  public void layoutContainer(Container parent) {
    synchronized (parent.getTreeLock()) {
      Insets insets = parent.getInsets();
      int ncomponents = parent.getComponentCount();
      int nrows = rows;
      int ncols = cols;
      boolean ltr = parent.getComponentOrientation().isLeftToRight();

      if (ncomponents == 0) {
        return;
      }
      if (nrows > 0) {
        ncols = (ncomponents + nrows - 1) / nrows;
      } else {
        nrows = (ncomponents + ncols - 1) / ncols;
      }
      int w = parent.width - (insets.left + insets.right);
      int h = parent.height - (insets.top + insets.bottom);
      w = (w - (ncols - 1) * hgap) / ncols;
      h = (h - (nrows - 1) * vgap) / nrows;

      if (ltr) {
        for (int c = 0, x = insets.left; c < ncols; c++, x += w + hgap) {
          for (int r = 0, y = insets.top; r < nrows; r++, y += h + vgap) {
            int i = r * ncols + c;
            if (i < ncomponents) {
              parent.getComponent(i).setBounds(x, y, w, h);
            }
          }
        }
      } else {
        for (int c = 0, x = parent.width - insets.right - w; c < ncols; c++, x -= w + hgap) {
          for (int r = 0, y = insets.top; r < nrows; r++, y += h + vgap) {
            int i = r * ncols + c;
            if (i < ncomponents) {
              parent.getComponent(i).setBounds(x, y, w, h);
            }
          }
        }
      }
    }
  }