コード例 #1
0
 private int getOrientation(Container container) {
   if (!useOrientation) {
     return SwingConstants.CENTER;
   }
   if (container.getComponentOrientation().isLeftToRight()) {
     return orientation;
   }
   switch (orientation) {
     case SwingConstants.LEFT:
       return SwingConstants.RIGHT;
     case SwingConstants.RIGHT:
       return SwingConstants.LEFT;
     case SwingConstants.CENTER:
       return SwingConstants.CENTER;
   }
   return SwingConstants.LEFT;
 }
コード例 #2
0
    public void layoutContainer(Container container) {
      Component[] children = container.getComponents();

      if (children != null && children.length > 0) {
        int numChildren = children.length;
        Insets insets = container.getInsets();
        int maxWidth = 0;
        int maxHeight = 0;
        int totalButtonWidth = 0;
        int x = 0;
        int xOffset = 0;
        boolean ltr = container.getComponentOrientation().isLeftToRight();
        boolean reverse = (ltr) ? reverseButtons : !reverseButtons;

        for (int counter = 0; counter < numChildren; counter++) {
          Dimension pref = children[counter].getPreferredSize();
          maxWidth = Math.max(maxWidth, pref.width);
          maxHeight = Math.max(maxHeight, pref.height);
          totalButtonWidth += pref.width;
        }
        if (getSyncAllWidths()) {
          totalButtonWidth = maxWidth * numChildren;
        }
        totalButtonWidth += (numChildren - 1) * padding;

        switch (getOrientation(container)) {
          case SwingConstants.LEFT:
            x = insets.left;
            break;
          case SwingConstants.RIGHT:
            x = container.getWidth() - insets.right - totalButtonWidth;
            break;
          case SwingConstants.CENTER:
            if (getCentersChildren() || numChildren < 2) {
              x = (container.getWidth() - totalButtonWidth) / 2;
            } else {
              x = insets.left;
              if (getSyncAllWidths()) {
                xOffset =
                    (container.getWidth() - insets.left - insets.right - totalButtonWidth)
                            / (numChildren - 1)
                        + maxWidth;
              } else {
                xOffset =
                    (container.getWidth() - insets.left - insets.right - totalButtonWidth)
                        / (numChildren - 1);
              }
            }
            break;
        }

        for (int counter = 0; counter < numChildren; counter++) {
          int index = (reverse) ? numChildren - counter - 1 : counter;
          Dimension pref = children[index].getPreferredSize();

          if (getSyncAllWidths()) {
            children[index].setBounds(x, insets.top, maxWidth, maxHeight);
          } else {
            children[index].setBounds(x, insets.top, pref.width, pref.height);
          }
          if (xOffset != 0) {
            x += xOffset;
          } else {
            x += children[index].getWidth() + padding;
          }
        }
      }
    }