⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gridbaglayout.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                                  break;                                }                              else                                {                                  constraints.gridy = Math.max (constraints.gridy,                                                                lastConstraints.gridy + Math.max (1, lastConstraints.gridheight));                                }                            }                        }                    }                  constraints.gridheight = max_y - constraints.gridy;                }              else if (constraints.gridheight == GridBagConstraints.RELATIVE)                {                  constraints.gridheight = max_y - constraints.gridy - 1;                }              // Re-sort              sortedByHeight.remove(sortedByHeight.indexOf(component));              sortBySpan(component, constraints.gridheight, sortedByHeight, false);            }        } // end of STEP 2      // STEP 3: Determine sizes and weights for columns.      for (int i = 0; i < sortedByWidth.size(); i++)        {          Component component = (Component) sortedByWidth.get(i);			          // If component is not visible we dont have to care about it.          if (!component.isVisible())            continue;          GridBagConstraints constraints = lookupInternalConstraints (component);          int width = (sizeflag == PREFERREDSIZE) ?                      component.getPreferredSize().width :                      component.getMinimumSize().width;          if(constraints.insets != null)            width += constraints.insets.left + constraints.insets.right;          width += constraints.ipadx;          distributeSizeAndWeight(width,                                  constraints.weightx,                                   constraints.gridx,                                  constraints.gridwidth,                                  info.colWidths,                                  info.colWeights);        } // end of STEP 3      // STEP 4: Determine sizes and weights for rows.      for (int i = 0; i < sortedByHeight.size(); i++)        {          Component component = (Component) sortedByHeight.get(i);			          // If component is not visible we dont have to care about it.          if (!component.isVisible())            continue;          GridBagConstraints constraints = lookupInternalConstraints (component);          int height = (sizeflag == PREFERREDSIZE) ?                       component.getPreferredSize().height :                       component.getMinimumSize().height;          if(constraints.insets != null)            height += constraints.insets.top + constraints.insets.bottom;          height += constraints.ipady;          distributeSizeAndWeight(height,                                  constraints.weighty,                                   constraints.gridy,                                  constraints.gridheight,                                  info.rowHeights,                                  info.rowWeights);        } // end of STEP 4      // Adjust cell sizes iff parent size not zero.      if (parentDim.width > 0 && parentDim.height > 0)        {          calcCellSizes (info.colWidths, info.colWeights, parentDim.width);          calcCellSizes (info.rowHeights, info.rowWeights, parentDim.height);        }      int totalWidth = sumIntArray(info.colWidths);      int totalHeight = sumIntArray(info.rowHeights);      // Make sure pos_x and pos_y are never negative.      if (totalWidth >= parentDim.width)        info.pos_x = parentInsets.left;      else        info.pos_x = parentInsets.left + (parentDim.width - totalWidth) / 2;      if (totalHeight >= parentDim.height)        info.pos_y = parentInsets.top;      else        info.pos_y = parentInsets.top + (parentDim.height - totalHeight) / 2;      // DEBUG      //dumpLayoutInfo (info);      return info;    }    /**     * Obsolete.     */    protected Dimension GetMinSize (Container parent, GridBagLayoutInfo info)    {      if (parent == null || info == null)        return new Dimension (0, 0);      Insets insets = parent.getInsets();      int width = sumIntArray (info.colWidths) + insets.left + insets.right;      int height = sumIntArray (info.rowHeights) + insets.top + insets.bottom;      return new Dimension (width, height);    }    /**     * @since 1.4     */    protected Dimension getMinSize (Container parent, GridBagLayoutInfo info)    {      return GetMinSize (parent, info);    }    /**     * Helper method used by GetLayoutInfo to keep components sorted, either     * by gridwidth or gridheight.     *     * @param component   Component to add to the sorted list.     * @param span        Either the component's gridwidth or gridheight.     * @param list        <code>ArrayList</code> of components, sorted by     *                    their span.     * @param sortByWidth Flag indicating sorting index. If true, sort by     *                    width. Otherwise, sort by height.     * FIXME: Use a better sorting algorithm.     */    private void sortBySpan (Component component, int span, ArrayList list, boolean sortByWidth)    {      if (span == GridBagConstraints.REMAINDER          || span == GridBagConstraints.RELATIVE)        {          // Put all RELATIVE and REMAINDER components at the end.          list.add(component);        }      else        {          int i = 0;          if (list.size() > 0)            {              GridBagConstraints gbc = lookupInternalConstraints((Component) list.get(i));              int otherspan = sortByWidth ?                              gbc.gridwidth :                              gbc.gridheight;              while (otherspan != GridBagConstraints.REMAINDER                     && otherspan != GridBagConstraints.RELATIVE                     && span >= otherspan)                {                  i++;                  if (i < list.size())                    {                      gbc = lookupInternalConstraints((Component) list.get(i));                      otherspan = sortByWidth ?                                  gbc.gridwidth :                                  gbc.gridheight;                    }                  else                    break;                }            }          list.add(i, component);        }    }    /**     * Helper method used by GetLayoutInfo to distribute a component's size     * and weight.     *     * @param size    Preferred size of component, with inset and padding     *                already added.     * @param weight  Weight of component.     * @param start   Starting position of component. Either     *                constraints.gridx or gridy.     * @param span    Span of component. either contraints.gridwidth or     *                gridheight.     * @param sizes   Sizes of rows or columns.     * @param weights Weights of rows or columns.     */    private void distributeSizeAndWeight (int size, double weight,                                          int start, int span,                                          int[] sizes, double[] weights)    {      if (span == 1)        {          sizes[start] = Math.max(sizes[start], size);          weights[start] = Math.max(weights[start], weight);        }      else if (span > 1)        {          int numOccupied = span;          int lastOccupied = -1;          for(int i = start; i < start + span; i++)            {              if (sizes[i] == 0.0)                numOccupied--;              else                {                  size -= sizes[i];                  lastOccupied = i;                }            }          // A component needs to occupy at least one row.          if(numOccupied == 0)            sizes[start + span - 1] = size;          else if (size > 0)            sizes[lastOccupied] += size;          calcCellWeights(weight, weights, start, span);        }    }    /**     * Helper method used by GetLayoutInfo to calculate weight distribution.     * @param weight  Weight of component.     * @param weights Weights of rows/columns.     * @param start   Starting position of component in grid (gridx/gridy).     * @param span    Span of component (gridwidth/gridheight).     */    private void calcCellWeights (double weight, double[] weights, int start, int span)    {      double totalWeight = 0.0;      for(int k = start; k < start + span; k++)        totalWeight += weights[k];      if(weight > totalWeight)        {          if (totalWeight == 0.0)            {              weights[start + span - 1] += weight;            }          else            {              double diff = weight - totalWeight ;              double remaining = diff;              for(int k = start; k < start + span; k++)                {                  double extraWeight = diff * weights[k] / totalWeight;                  weights[k] += extraWeight;                  remaining -= extraWeight;                }               if (remaining > 0.0 && weights[start + span - 1] != 0.0)                {                  weights[start + span - 1] += remaining;                }            }        }    }    /**     * Helper method used by GetLayoutInfo to distribute extra space     * based on weight distribution.     *     * @param sizes   Sizes of rows/columns.     * @param weights Weights of rows/columns.     * @param range   Dimension of container.     */    private void calcCellSizes (int[] sizes, double[] weights, int range)    {      int totalSize = sumIntArray (sizes);      double totalWeight = sumDoubleArray (weights);      int diff = range - totalSize;      if (diff == 0)        return;      for (int i = 0; i < sizes.length; i++)        {          int newsize = (int) (sizes[i] + (((double) diff) * weights [i] / totalWeight ));          if (newsize > 0)            sizes[i] = newsize;        }    }    private void dumpLayoutInfo (GridBagLayoutInfo info)    {	System.out.println ("GridBagLayoutInfo:");	System.out.println ("cols: " + info.cols + ", rows: " + info.rows);	System.out.print ("colWidths: ");	dumpArray(info.colWidths);	System.out.print ("rowHeights: ");	dumpArray(info.rowHeights);	System.out.print ("colWeights: ");	dumpArray(info.colWeights);	System.out.print ("rowWeights: ");	dumpArray(info.rowWeights);    }    private void dumpArray(int[] array)    {	String sep = "";	for(int i = 0; i < array.length; i++)	{	    System.out.print(sep);	    System.out.print(array[i]);	    sep = ", ";	}	System.out.println();    }    private void dumpArray(double[] array)    {	String sep = "";	for(int i = 0; i < array.length; i++)	{	    System.out.print(sep);	    System.out.print(array[i]);	    sep = ", ";	}	System.out.println();    }      /**     * @since 1.4     */    protected void arrangeGrid (Container parent)    {      ArrangeGrid (parent);    }    /**     * @since 1.4     */    protected GridBagLayoutInfo getLayoutInfo (Container parent, int sizeflag)    {      return GetLayoutInfo (parent, sizeflag);    }    /**     * @since 1.4     */    protected void adjustForGravity (GridBagConstraints gbc, Rectangle rect)    {      AdjustForGravity (gbc, rect);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -