Пример #1
0
 public static void main(String[] args) {
   JButton button = new JButton();
   button.putClientProperty("JButton.buttonType", "metal");
   JButton button2 = new JButton();
   LayoutStyle style = new AquaLayoutStyle();
   int gap =
       style.getPreferredGap(button, button2, LayoutStyle.RELATED, SwingConstants.EAST, null);
   System.err.println("gap= " + gap);
   button.putClientProperty("JButton.buttonType", "square");
   button2.putClientProperty("JButton.buttonType", "square");
   gap = style.getPreferredGap(button, button2, LayoutStyle.RELATED, SwingConstants.EAST, null);
   System.err.println("gap= " + gap);
 }
Пример #2
0
  /**
   * Returns the amount of space to use between two components. The return value indicates the
   * distance to place <code>component2</code> relative to <code>component1</code>. For example, the
   * following returns the amount of space to place between <code>component2</code> and <code>
   * component1</code> when <code>component2</code> is placed vertically above <code>component1
   * </code>:
   *
   * <pre>
   *   int gap = getPreferredGap(component1, component2,
   *                             LayoutStyle.RELATED,
   *                             SwingConstants.NORTH, parent);
   * </pre>
   *
   * The <code>type</code> parameter indicates the type of gap being requested. It can be one of the
   * following values:
   *
   * <table>
   * <tr><td><code>RELATED</code>
   *     <td>If the two components will be contained in
   *         the same parent and are showing similar logically related
   *         items, use <code>RELATED</code>.
   * <tr><td><code>UNRELATED</code>
   *     <td>If the two components will be
   *          contained in the same parent but show logically unrelated items
   *          use <code>UNRELATED</code>.
   * <tr><td><code>INDENT</code>
   *     <td>Used to obtain the preferred distance to indent a component
   *         relative to another.  For example, if you want to horizontally
   *         indent a JCheckBox relative to a JLabel use <code>INDENT</code>.
   *         This is only useful for the horizontal axis.
   * </table>
   *
   * <p>It's important to note that some look and feels may not distinguish between <code>RELATED
   * </code> and <code>UNRELATED</code>.
   *
   * <p>The return value is not intended to take into account the current size and position of
   * <code>component2</code> or <code>component1</code>. The return value may take into
   * consideration various properties of the components. For example, the space may vary based on
   * font size, or the preferred size of the component.
   *
   * @param component1 the <code>JComponent</code> <code>component2</code> is being placed relative
   *     to
   * @param component2 the <code>JComponent</code> being placed
   * @param type how the two components are being placed
   * @param position the position <code>component2</code> is being placed relative to <code>
   *     component1</code>; one of <code>SwingConstants.NORTH</code>, <code>SwingConstants.SOUTH
   *     </code>, <code>SwingConstants.EAST</code> or <code>SwingConstants.WEST</code>
   * @param parent the parent of <code>component2</code>; this may differ from the actual parent and
   *     may be null
   * @return the amount of space to place between the two components
   * @throws IllegalArgumentException if <code>position</code> is not one of <code>
   *     SwingConstants.NORTH</code>, <code>SwingConstants.SOUTH</code>, <code>SwingConstants.EAST
   *     </code> or <code>SwingConstants.WEST</code>; <code>type</code> not one of <code>INDENT
   *     </code>, <code>RELATED</code> or <code>UNRELATED</code>; or <code>component1</code> or
   *     <code>component2</code> is null
   */
  public int getPreferredGap(
      JComponent component1, JComponent component2, int type, int position, Container parent) {
    // Check args
    super.getPreferredGap(component1, component2, type, position, parent);

    int result;

    // Compute gap
    if (type == INDENT) {
      // Compute gap
      if (position == SwingConstants.EAST || position == SwingConstants.WEST) {
        int gap = getButtonChildIndent(component1, position);
        if (gap != 0) {
          return gap;
        }
      }
      int sizeStyle = getSizeStyle(component1);
      Insets gap1 = getPreferredGap(component1, type, sizeStyle);
      switch (position) {
        case SwingConstants.NORTH:
          result = gap1.bottom;
          break;
        case SwingConstants.SOUTH:
          result = gap1.top;
          break;
        case SwingConstants.EAST:
          result = gap1.left;
          break;
        case SwingConstants.WEST:
        default:
          result = gap1.right;
          break;
      }
      int raw = result;
      // Compensate for visual margin
      Insets visualMargin2 = getVisualMargin(component2);
      switch (position) {
        case SwingConstants.NORTH:
          result -= visualMargin2.bottom;
          break;
        case SwingConstants.SOUTH:
          result -= visualMargin2.top;
          break;
        case SwingConstants.EAST:
          result -= visualMargin2.left;
          break;
        case SwingConstants.WEST:
          result -= visualMargin2.right;
        default:
          break;
      }
    } else {
      // Compute gap
      int sizeStyle = Math.min(getSizeStyle(component1), getSizeStyle(component2));
      Insets gap1 = getPreferredGap(component1, type, sizeStyle);
      Insets gap2 = getPreferredGap(component2, type, sizeStyle);
      switch (position) {
        case SwingConstants.NORTH:
          result = Math.max(gap1.top, gap2.bottom);
          break;
        case SwingConstants.SOUTH:
          result = Math.max(gap1.bottom, gap2.top);
          break;
        case SwingConstants.EAST:
          result = Math.max(gap1.right, gap2.left);
          break;
        case SwingConstants.WEST:
        default:
          result = Math.max(gap1.left, gap2.right);
          break;
      }

      // Compensate for visual margin
      Insets visualMargin1 = getVisualMargin(component1);
      Insets visualMargin2 = getVisualMargin(component2);

      switch (position) {
        case SwingConstants.NORTH:
          result -= visualMargin1.top + visualMargin2.bottom;
          break;
        case SwingConstants.SOUTH:
          result -= visualMargin1.bottom + visualMargin2.top;
          break;
        case SwingConstants.EAST:
          result -= visualMargin1.right + visualMargin2.left;
          break;
        case SwingConstants.WEST:
          result -= visualMargin1.left + visualMargin2.right;
        default:
          break;
      }
    }

    // Aqua does not support negative gaps, because all its components are
    // opaque
    return Math.max(0, result);
  }