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

📄 tablelayout.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    // Calculate offsets of each column (done for effeciency)
    columnOffset = new int[numColumn + 1];
    columnOffset[0] = inset.left;

    for (counter = 0; counter < numColumn; counter++)
        columnOffset[counter + 1] = 
            columnOffset[counter] + columnSize[counter];

    // Calculate offsets of each row (done for effeciency)
    rowOffset = new int[numRow + 1];
    rowOffset[0] = inset.top;

    for (counter = 0; counter < numRow; counter++)
        rowOffset[counter + 1] =
            rowOffset[counter] + rowSize[counter];

    // Indicate that the size of the cells are known for the container's
    // current size
    dirty = false;
    oldWidth = totalWidth;
    oldHeight = totalHeight;
}



//******************************************************************************
//** java.awt.event.LayoutManager methods                                    ***
//******************************************************************************



/**
 * To lay out the specified container using this layout.  This method reshapes
 * the components in the specified target container in order to satisfy the
 * constraints of all components.
 *
 * <p>User code should not have to call this method directly.</p>
 *
 * @param container    container being served by this layout manager
 */

public void layoutContainer (Container container)
{
    int x, y; // Coordinates of the currnet component in pixels
    int w, h; // Width and height of the current component in pixels

    // Calculate sizes if container has changed size or components were added
    Dimension d = container.getSize();

    if (dirty || (d.width != oldWidth) || (d.height != oldHeight))
        calculateSize (container);

    // Get components
    Component component[] = container.getComponents();

    // Layout components
    for (int counter = 0; counter < component.length; counter++)
    {
        try
        {
            // Get the entry entry for the next component
            ListIterator iterator = list.listIterator(0);
            Entry entry = null;
	    
            while (iterator.hasNext())
            {
                entry = (Entry) iterator.next();
                
                if (entry.component == component[counter])
                    break;
                else
                    entry = null;
            }

            // Skip any components that have not been place in a specific cell
            if (entry == null)
                break;

            // Does the entry occupy a single cell
            if (entry.singleCell)
            {
                // The following block of code has been optimized so that the
                // preferred size of the component is only obtained if it is
                // needed.  There are components in which the getPreferredSize
                // method is extremely expensive, such as data driven controls
                // with a large amount of data.

                // Get the preferred size of the component
                int preferredWidth = 0;
                int preferredHeight = 0;

                if ((entry.hAlign != FULL) || (entry.vAlign != FULL))
                {
                    Dimension preferredSize =
                        component[counter].getPreferredSize();

                    preferredWidth = preferredSize.width;
                    preferredHeight = preferredSize.height;
                }

                // Determine cell width and height
                int cellWidth = columnSize[entry.col1];
                int cellHeight = rowSize[entry.row1];

                // Determine the width of the component
                if ((entry.hAlign == FULL) ||
                    (cellWidth < preferredWidth))
                    // Use the width of the cell
                    w = cellWidth;
                else
                    // Use the prefered width of the component
                    w = preferredWidth;

                // Determine left and right boarders
                switch (entry.hAlign)
                {
                    case LEFT :
                        // Align left side along left edge of cell
                        x = columnOffset[entry.col1];
                    break;

                    case RIGHT :
                        // Align right side along right edge of cell
                        x = columnOffset[entry.col1 + 1] - w;
                    break;

                    case CENTER :
                        // Center justify component
                        x = columnOffset[entry.col1] + ((cellWidth - w) >> 1);
                    break;

                    case FULL :
                        // Align left side along left edge of cell
                        x = columnOffset[entry.col1];
                    break;

                    default :
                        // This is a never should happen case, but just in case
                        x = 0;
                }

                // Determine the height of the component
                if ((entry.vAlign == FULL) ||
                    (cellHeight < preferredHeight))
                    // Use the height of the cell
                    h = cellHeight;
                else
                    // Use the prefered height of the component
                    h = preferredHeight;

                // Determine top and bottom boarders
                switch (entry.vAlign)
                {
                    case TOP :
                        // Align top side along top edge of cell
                        y = rowOffset[entry.row1];
                    break;

                    case BOTTOM :
                        // Align right side along right edge of cell
                        y = rowOffset[entry.row1 + 1] - h;
                    break;

                    case CENTER :
                        // Center justify component
                        y = rowOffset[entry.row1] + ((cellHeight - h) >> 1);
                    break;

                    case FULL :
                        // Align right side along right edge of cell
                        y = rowOffset[entry.row1];
                    break;

                    default :
                        // This is a never should happen case, but just in case
                        y = 0;
                }
            }
            else
            {
                // Align left side with left boarder of first column
                x = columnOffset[entry.col1];

                // Align top side along top edge of first row
                y = rowOffset[entry.row1];

                // Align right side with right boarder of second column
                w = columnOffset[entry.col2 + 1] -
                    columnOffset[entry.col1];

                // Align bottom side with bottom boarder of second row
                h = rowOffset[entry.row2 + 1] - rowOffset[entry.row1];
            }

            // Move and resize component
            component[counter].setBounds (x, y, w, h);
        }
        catch (Exception error)
        {
            // If any error occurs, skip this component
            continue;
        }
    }
}



/**
 * Determines the preferred size of the container argument using this layout.
 * The preferred size is the smallest size that, if used for the container's
 * size, will ensure that all components are at least as large as their
 * preferred size.  This method cannot guarantee that all components will be
 * their preferred size.  For example, if component A and component B are each
 * allocate half of the container's width and component A wants to be 10 pixels
 * wide while component B wants to be 100 pixels wide, they cannot both be
 * accommodated.  Since in general components rather be larger than their
 * preferred size instead of smaller, component B's request will be fulfilled.
 * The preferred size of the container would be 200 pixels.
 *
 * @param container    container being served by this layout manager
 *
 * @return a dimension indicating the container's preferred size
 */

public Dimension preferredLayoutSize (Container container)
{
    Dimension size;       // Preferred size of current component
    int scaledWidth = 0;  // Preferred width of scalled components
    int scaledHeight = 0; // Preferred height of scalled components
    int temp;             // Temporary variable used to compare sizes
    int counter;          // Counting variable

    // Determine percentage of space allocated to fill components.  This is
    // one minus the sum of all scalable components.
    double fillWidthRatio = 1.0;
    double fillHeightRatio = 1.0;
    int    numFillWidth = 0;
    int    numFillHeight = 0;

    for (counter = 0; counter < columnSpec.length; counter++)
        if ((columnSpec[counter] > 0.0) && (columnSpec[counter] < 1.0))
            fillWidthRatio -= columnSpec[counter];
        else if (columnSpec[counter] == FILL)
            numFillWidth++;

    for (counter = 0; counter < rowSpec.length; counter++)
        if ((rowSpec[counter] > 0.0) && (rowSpec[counter] < 1.0))
            fillHeightRatio -= rowSpec[counter];
        else if (rowSpec[counter] == FILL)
            numFillHeight++;

    // Adjust fill ratios to reflect number of fill rows/columns
    if (numFillWidth > 1)
        fillWidthRatio /= numFillWidth;

    if (numFillHeight > 1)
        fillHeightRatio /= numFillHeight;

    // Cap fill ratio bottoms to 0.0
    if (fillWidthRatio < 0.0)
        fillWidthRatio = 0.0;

    if (fillHeightRatio < 0.0)
        fillHeightRatio = 0.0;

    // Calculate preferred/minimum column widths
    int columnPrefMin[] = new int[columnSpec.length];

    for (counter = 0; counter < columnSpec.length; counter++)
        // Is the current column a preferred/minimum size
        if ((columnSpec[counter] == PREFERRED) ||
            (columnSpec[counter] == MINIMUM))
        {
            // Assume a maximum width of zero
            int maxWidth = 0;

            // Find maximum preferred/minimum width of all components completely
            // contained within this column
            ListIterator iterator = list.listIterator(0);

            while (iterator.hasNext())
            {
                Entry entry = (Entry) iterator.next();
                
                if ((entry.col1 == counter) && (entry.col2 == counter))
                {
                    Dimension p = (columnSpec[counter] == PREFERRED) ?
                        entry.component.getPreferredSize() :
                        entry.component.getMinimumSize();

                    int width = (p == null) ? 0 : p.width;

                    if (maxWidth < width)
                        maxWidth = width;
                }
            }

            // Set column's preferred/minimum width
            columnPrefMin[counter] = maxWidth;
        }


    // Calculate preferred/minimum row heights
    int rowPrefMin[] = new int[rowSpec.length];

    for (counter = 0; counter < rowSpec.length; counter++)
        // Is the current row a preferred/minimum size
        if ((rowSpec[counter] == PREFERRED) ||
            (rowSpec[counter] == MINIMUM))
        {
            // Assume a maximum height of zero
            int maxHeight = 0;

            // Find maximum preferred height of all components completely
            // contained within this row
            ListIterator iterator = list.listIterator(0);

            while (iterator.hasNext())
            {
                Entry entry = (Entry) iterator.next();
                
                if ((entry.row1 == counter) && (entry.row1 == counter))
                {
                    Dimension p = (rowSpec[counter] == PREFERRED) ?
                        entry.component.getPreferredSize() :
                        entry.component.getMinimumSize();

                    int height = (p == null) ? 0 : p.height;

                    if (maxHeight < height)
                        maxHeight = height;
                }
            }

            // Add preferred height
            rowPrefMin[counter] += maxHeight;
        }

    // Find maximum preferred size of all scaled components
    ListIterator iterator = list.listIterator(0);
    
    while (iterator.hasNext())
    {
        // Get next entry
        Entry entry = (Entry) iterator.next();

        // Make sure entry is in valid rows and columns
        if ((entry.col1 < 0) || (entry.col1 >= columnSpec.length) ||
                                (entry.col2 >= columnSpec.length) ||
            (entry.row1 < 0) || (entry.row1 >= rowSpec.length)    ||
                                (entry.row2 >= rowSpec.length))
        {
            // Skip the bad component
            continue;
        }

        // Get preferred size of current component
        size = entry.component.getPreferredSize();

        // Calculate portion of component that is not absolutely sized
        int scalableWidth = size.width;
        int scalableHeight = size.height;

        for (counter = entry.col1; counter <= entry.col2; counter++)
            if (columnSpec[counter] >= 1.0)
                scalableWidth -= columnSpec[counter];
            else if ((columnSpec[counter] == PREFERRED) ||
                     (columnSpec[counter] == MINIMUM))
            {
                scalableWidth -= columnPrefMin[counter];
            }

        for (counter = entry.row1; counter <= entry.row2; counter++)
            if (rowSpec[counter] >= 1.0)
                scalableHeight -= rowSpec[counter];
            else if ((rowSpec[counter] == PREFERRED) ||
                     (rowSpec[counter] == MINIMUM))
            {
                scalableHeight -= rowPrefMin[counter];
            }

        //----------------------------------------------------------------------

        // Determine total percentage of scalable space that the component
        // occupies by adding the relative columns and the fill columns
        double relativeWidth = 0.0;

        for (counter = entry.col1; counter <= entry.col2; counter++)
        {
            // Column is scaled
            if ((columnSpec[counter] > 0.0) && (columnSpec[counter] < 1.0))
                // Add scaled size to relativeWidth
                relativeWidth += columnSpec[counter];
            // Column is fill
            else if ((columnSpec[counter] == FILL) && (fillWidthRatio != 0.0))
                // Add fill size to relativeWidth
                relativeWidth += fillWidthRatio;
        }

        // Determine the total scaled width as estimated by this component
        if (relativeWidth == 0)
            temp = 0;
        else
            temp = (int) (scalableWidth / relativeWidth + 0.5);

        // If the container needs to be bigger, make it so
        if (scaledWidth < temp)
            scaledWidth = temp;

        //----------------------------------------------------------------------

⌨️ 快捷键说明

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