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

📄 tablelayout.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
 *
 * @return a string representing the columns and row sizes in the form
 *         "{{col0, col1, col2, ..., colN}, {row0, row1, row2, ..., rowM}}"
 */

public String toString ()
{
    int counter;

    String value = "TableLayout {{";

    if (columnSpec.length > 0)
    {
        for (counter = 0; counter < columnSpec.length - 1; counter++)
            value += columnSpec[counter] + ", ";

        value += columnSpec[columnSpec.length - 1] + "}, {";
    }
    else
        value += "}, {";

    if (rowSpec.length > 0)
    {
        for (counter = 0; counter < rowSpec.length - 1; counter++)
            value += rowSpec[counter] + ", ";

        value += rowSpec[rowSpec.length - 1] + "}}";
    }
    else
        value += "}}";

    return value;
}



/**
 * Draws a grid on the given container.  This is useful for seeing where the
 * rows and columns go.  In the container's paint method, call this method.
 *
 * @param container    container using this TableLayout
 * @param g            graphics content of container (can be offscreen)
 */

public void drawGrid (Container container, Graphics g)
{
    
    // Calculate the sizes of the rows and columns
    Dimension d = container.getSize();

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

    // Initialize y
    int y = 0;

    for (int row = 0; row < rowSize.length; row++)
    {
        // Initialize x
        int x = 0;

        for (int column = 0; column < columnSize.length; column++)
        {
            // Use a random color to make things easy to see
            Color color = new Color((int) (Math.random() * 0xFFFFFFL));
            g.setColor (color);

            // Draw the cell as a solid rectangle
            g.fillRect (x, y, columnSize[column], rowSize[row]);

            // Increment x
            x += columnSize[column];
        }

        // Increment y
        y += rowSize[row];
    }
}



/**
 * Determines whether or not there are any hidden components.  A hidden
 * component is one that will not be shown with this layout's current
 * configuration.  Such a component is, at least partly, in an invalid row
 * or column.  For example, on a table with five rows, row -1 and row 5 are both
 * invalid.  Valid rows are 0 through 4, inclusively.
 *
 * @return    True, if there are any hidden components.  False, otherwise.
 *
 * @see #overlapping
 */

public boolean hidden ()
{
    // Assume no components are hidden
    boolean hidden = false;

    // Check all components
    ListIterator iterator = list.listIterator(0);
    
    while (iterator.hasNext())
    {
        // Get next entry
        Entry entry = (Entry) iterator.next();

        // Is this component valid
        if ((entry.row1 < 0) || (entry.col1 < 0) ||
            (entry.row2 > rowSpec.length) ||
            (entry.col2 > columnSpec.length))
        {
            hidden = true;
            break;
        }
    }

    return hidden;
}



/**
 * Determines whether or not there are any overlapping components.  Two
 * components overlap if they cover at least one common cell.
 *
 * @return    True, if there are any overlapping components.  False, otherwise.
 *
 * @see #hidden
 */

public boolean overlapping ()
{
    // Count contraints
    int numEntry = list.size();

    // If there are no components, they can't be overlapping
    if (numEntry == 0)
        return false;

    // Assume no components are overlapping
    boolean overlapping = false;

    // Put entries in an array
    Entry entry[] = (Entry[]) list.toArray(new Entry[numEntry]);

    // Check all components
    for (int knowUnique = 1; knowUnique < numEntry; knowUnique++)
        for (int checking = knowUnique - 1; checking >= 0; checking--)
            if
               (
                (
                 (entry[checking].col1 >= entry[knowUnique].col1) &&
                 (entry[checking].col1 <= entry[knowUnique].col2) &&
                 (entry[checking].row1 >= entry[knowUnique].row1) &&
                 (entry[checking].row1 <= entry[knowUnique].row2)
                )
                ||
                (
                 (entry[checking].col2 >= entry[knowUnique].col1) &&
                 (entry[checking].col2 <= entry[knowUnique].col2) &&
                 (entry[checking].row2 >= entry[knowUnique].row1) &&
                 (entry[checking].row2 <= entry[knowUnique].row2)
                )
               )
            {
                overlapping = true;
                break;
            }

    return overlapping;
}



/**
 * Calculates the sizes of the rows and columns based on the absolute and
 * relative sizes specified in <code>rowSpec</code> and <code>columnSpec</code>
 * and the size of the container.  The result is stored in <code>rowSize</code>
 * and <code>columnSize</code>.
 *
 * @param container    container using this TableLayout
 */

protected void calculateSize (Container container)
{
    int counter; // Counting variable;

    // Get number of rows and columns
    int numColumn = columnSpec.length;
    int numRow = rowSpec.length;

    // Create array to hold actual sizes in pixels
    columnSize = new int[numColumn];
    rowSize = new int[numRow];

    // Get the container's insets
    Insets inset = container.getInsets();

    // Get the size of the container's available space
    Dimension d = container.getSize();
    int totalWidth = d.width - inset.left - inset.right;
    int totalHeight = d.height - inset.top - inset.bottom;

    // Initially, the available space is the total space
    int availableWidth = totalWidth;
    int availableHeight = totalHeight;

    // Assign absolute widths; this reduces available width
    for (counter = 0; counter < numColumn; counter++)
        // Is the current column an absolue size
        if ((columnSpec[counter] >= 1.0) || (columnSpec[counter] == 0.0))
        {
            // Assign absolute width
            columnSize[counter] = (int) (columnSpec[counter] + 0.5);

            // Reduce available width
            availableWidth -= columnSize[counter];
        }

    // Assign absolute heights; this reduces available height
    for (counter = 0; counter < numRow; counter++)
        // Is the current column an absolue size
        if ((rowSpec[counter] >= 1.0) || (rowSpec[counter] == 0.0))
        {
            // Assign absolute width
            rowSize[counter] = (int) (rowSpec[counter] + 0.5);

            // Reduce available width
            availableHeight -= rowSize[counter];
        }

    // Assign preferred and minimum widths; this reduces available width.
    // Assignment of preferred/minimum with is like assignment of absolute
    // widths except that each column must determine the maximum
    // preferred/minimum width of the components that are completely contained
    // within the column.
    for (counter = 0; counter < numColumn; counter++)
        // Is the current column a preferred size
        if ((columnSpec[counter] == PREFERRED) ||
            (columnSpec[counter] == MINIMUM))
        {
            // Assume a maximum width of zero
            int maxWidth = 0;

            // Find maximum preferred 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;
                }
            }

            // Assign preferred width
            columnSize[counter] = maxWidth;

            // Reduce available width
            availableWidth -= maxWidth;
        }

    // Assign preferred and minimum heights; this reduces available height.
    // Assignment of preferred/minimum with is like assignment of absolute
    // heights except that each row must determine the maximum
    // preferred/minimum height of the components that are completely contained
    // within the row.
    for (counter = 0; counter < numRow; counter++)
        // Is the current row a preferred 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.row2 == counter))
                {
                    Dimension p = (rowSpec[counter] == PREFERRED) ?
                        entry.component.getPreferredSize() :
                        entry.component.getMinimumSize();

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

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

            // Assign preferred height
            rowSize[counter] = maxHeight;

            // Reduce available height
            availableHeight -= maxHeight;
        }

    // Remember how much space is available for relatively sized cells
    int relativeWidth = availableWidth;
    int relativeHeight = availableHeight;

    // Make sure relativeWidth and relativeHeight are non-negative
    if (relativeWidth < 0)
        relativeWidth = 0;

    if (relativeHeight < 0)
        relativeHeight = 0;

    // Assign relative widths
    for (counter = 0; counter < numColumn; counter++)
        // Is the current column an relative size
        if ((columnSpec[counter] > 0.0) && (columnSpec[counter] < 1.0))
        {
            // Assign relative width
            columnSize[counter] =
                (int) (columnSpec[counter] * relativeWidth + 0.5);

            // Reduce available width
            availableWidth -= columnSize[counter];
        }

    // Assign relative widths
    for (counter = 0; counter < numRow; counter++)
        // Is the current column an relative size
        if ((rowSpec[counter] > 0.0) && (rowSpec[counter] < 1.0))
        {
            // Assign relative width
            rowSize[counter] = (int) (rowSpec[counter] * relativeHeight + 0.5);

            // Reduce available width
            availableHeight -= rowSize[counter];
        }

    // Make sure availableWidth and availableHeight are non-negative
    if (availableWidth < 0)
        availableWidth = 0;

    if (availableHeight < 0)
        availableHeight = 0;

    // Count the number of "fill" cells
    int numFillWidth = 0;
    int numFillHeight = 0;

    for (counter = 0; counter < numColumn; counter++)
        if (columnSpec[counter] == FILL)
            numFillWidth++;

    for (counter = 0; counter < numRow; counter++)
        if (rowSpec[counter] == FILL)
            numFillHeight++;

    // If numFillWidth (numFillHeight) is zero, the cooresponding if statements
    // will always evaluate to false and the division will not occur.

    // If there are more than one "fill" cell, slack may occur due to rounding
    // errors
    int slackWidth = availableWidth;
    int slackHeight = availableHeight;

    // Assign "fill" cells equal amounts of the remaining space
    for (counter = 0; counter < numColumn; counter++)
        if (columnSpec[counter] == FILL)
        {
            columnSize[counter] = availableWidth / numFillWidth;
            slackWidth -= columnSize[counter];
        }

    for (counter = 0; counter < numRow; counter++)
        if (rowSpec[counter] == FILL)
        {
            rowSize[counter] = availableHeight / numFillHeight;
            slackHeight -= rowSize[counter];
        }

    // Add slack to the last "fill" cell
    for (counter = numColumn - 1; counter >= 0; counter--)
    {
        if (columnSpec[counter] == FILL)
        {
            columnSize[counter] += slackWidth;
            break;
        }
    }

    for (counter = numRow - 1; counter >= 0; counter--)
    {
        if (rowSpec[counter] == FILL)
        {
            rowSize[counter] += slackHeight;
            break;
        }
    }

⌨️ 快捷键说明

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