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

📄 higlayout.java

📁 超市收银系统(运用JDBC连接ACCESS数据库) 界面美观,功能比较齐全
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    {
                        Component c = (Component) iComps.get(j);
                        int width = c.isVisible() ? c.getPreferredSize().width : invisible;
                        if(width > 0)
                        {
                            HIGConstraints constr = (HIGConstraints) components.get(c);
                            if (constr.w < 0)
                                width = -constr.w;
                            else
                                width += constr.wCorrection;
                        }
                        maxWidth = (width > maxWidth) ? width : maxWidth;
                    }
                }
                widths[i] = maxWidth;
            }
        }
        solveCycles(colWidths, widths);

        return widths;
    }

    private int[] calcPreferredHeights()
    {
        int[] heights = new int[rowCount + 1];
        for (int i = 1; i <= rowCount; i++) {
            if (rowHeights[i] > 0) {
                heights[i] = rowHeights[i];
            } else if (preferredHeights[i] > 0) {
                heights[i] = preferredHeights[i];
            } else {            
                ArrayList iComps = rowComponents[i];
                int maxHeight = 0;
                if (iComps != null)
                {
                    for (int j = iComps.size() - 1; j >= 0; j--)
                    {
                        Component c = (Component) iComps.get(j);
                        int height = c.isVisible() ? c.getPreferredSize().height : invisible;
                        if(height > 0)
                        {
                            HIGConstraints constr = (HIGConstraints) components.get(c);
                            if (constr.h < 0)
                                height = -constr.h;
                            else
                                height += constr.hCorrection;
                        }
                        maxHeight = (height > maxHeight) ? height : maxHeight;
                    }
                }
                heights[i] = maxHeight;
            }
        }
        solveCycles(rowHeights, heights);
        return heights;
    }

    private void distributeSizeDifference(int desiredLength, int[] lengths, int[] minLengths,
                                          int[] weights, int weightSum)
    {
        int preferred = 0;
        int newLength;
        for (int i = lengths.length - 1; i > 0; i--) preferred += lengths[i];

        double unit = ((double) (desiredLength - preferred)) / (double) weightSum;

        for (int i = lengths.length - 1; i > 0; i--)
        {
            newLength = lengths[i] + (int) (unit * (double) weights[i]);
            lengths[i] = (newLength > minLengths[i]) ? newLength : minLengths[i];
        }
    }

    /**
     * Calculates the preferred size dimensions for the specified
     * container given the components in the specified parent container.
     * @param parent the component to be laid out
     *
     * @see #minimumLayoutSize
     */
    public Dimension preferredLayoutSize(Container target)
    {
        synchronized (target.getTreeLock())
        {
            if (cachePreferredLayoutSize != null) return cachePreferredLayoutSize;
            final int[] prefColWidths = calcPreferredWidths();
            final int[] prefRowHeights = calcPreferredHeights();
            Insets insets = target.getInsets();
            int w = insets.left + insets.right;
            int h = insets.top + insets.bottom;
            for (int i = 1; i <= colCount; i++) w += prefColWidths[i];
            for (int i = 1; i <= rowCount; i++) h += prefRowHeights[i];
            cachePreferredLayoutSize = new Dimension(w, h);
            return cachePreferredLayoutSize;
        }

    }

    public Dimension minimumLayoutSize(Container target)
    {
        synchronized (target.getTreeLock())
        {
            if (cacheMinimumLayoutSize != null) return cacheMinimumLayoutSize;
            final int[] minColWidths = calcMinWidths();
            final int[] minRowHeights = calcMinHeights();
            Insets insets = target.getInsets();
            int w = insets.left + insets.right;
            int h = insets.top + insets.bottom;
            for (int i = 1; i <= colCount; i++) w += minColWidths[i];
            for (int i = 1; i <= rowCount; i++) h += minRowHeights[i];
            cacheMinimumLayoutSize = new Dimension(w, h);
            return cacheMinimumLayoutSize;
        }
    }

    /**
     * returns array of x-coordinates of columns. First coordinate is stored in x[1]
     * Reference to this array is cached, so data should not be modified.
     */
    int[] getColumnsX(int targetWidth, Insets insets)
    {
        if (cacheColumnsX != null) return cacheColumnsX;
        int[] prefColWidths = calcPreferredWidths();
        int[] minColWidths = calcMinWidths();

        distributeSizeDifference(targetWidth, prefColWidths, minColWidths, widenWeights, widenWeightsSum);
        int x[] = new int[colCount + 2];
        x[1] = (insets == null) ? 0 : insets.left;

        for (int i = 2; i <= colCount + 1; i++) x[i] = x[i - 1] + prefColWidths[i - 1];
        cacheColumnsX = x;
        return x;
    }

    /**
     * returns array of y-coordinates of rows. First coordinate is stored in y[1].
     * Reference to this array is cached, so data should not be modified.
     */
    int[] getRowsY(int targetHeight, Insets insets)
    {
        if (cacheRowsY != null) return cacheRowsY;
        int[] prefRowHeights = calcPreferredHeights();
        int[] minRowHeights = calcMinHeights();

        distributeSizeDifference(targetHeight, prefRowHeights, minRowHeights, heightenWeights, heightenWeightsSum);
        int y[] = new int[rowCount + 2];
        y[1] = (insets == null) ? 0 : insets.top;

        for (int i = 2; i <= rowCount + 1; i++) y[i] = y[i - 1] + prefRowHeights[i - 1];
        cacheRowsY = y;
        return y;
    }

    public void layoutContainer(Container target)
    {
        synchronized (target.getTreeLock())
        {
            Dimension dimSize = target.getSize();
            int sizeWidth = dimSize.width;
            int sizeHeight = dimSize.height;
            Insets insets = target.getInsets();
            sizeWidth -= insets.left + insets.right;
            sizeHeight -= insets.top + insets.bottom;
            int x[] = getColumnsX(sizeWidth, insets);
            int y[] = getRowsY(sizeHeight, insets);

            Component comps[] = target.getComponents();
            for (int i = comps.length - 1; i >= 0; i--)
            {
                Component comp = comps[i];
                HIGConstraints c = (HIGConstraints) components.get(comp);
                if (c == null) continue;
                /* first we centre component into cell */
                Dimension dimPref = comp.getPreferredSize();
                int width = dimPref.width;
                int height = dimPref.height;
                int cellw;
                int cellh;
                if (c.w < 0)
                {
                    width = -c.w;
                    cellw = x[c.x + 1] - x[c.x];
                }
                else
                {
                    width += c.wCorrection;
                    cellw = x[c.x + c.w] - x[c.x];
                }
                if (c.h < 0)
                {
                    height = -c.h;
                    cellh = y[c.y + 1] - y[c.y];
                }
                else
                {
                    height += c.hCorrection;
                    cellh = y[c.y + c.h] - y[c.y];
                }


                boolean allowXSize = true;
                boolean allowYSize = true;
                /* I had intend to ensure that maximumSize is respected, but Swing components returns stupid maximumSize */
                /*
                Dimension dMax = comp.getMaximumSize();

                if(cellw > dMax.width) {
                    width = dMax.width;
                    allowXSize = false;
                }
                if(cellh > dMax.height) {
                    height = dMax.height;
                    allowYSize = false;
                } */
                float dw = ((float) (cellw - width)) / 2.0f;
                float dh = ((float) (cellh - height)) / 2.0f;
                float compx = (float) x[c.x] + dw;
                float compy = (float) y[c.y] + dh;

                /* now anchor to cell borders */
                String anchor = c.anchor;
                boolean xSize = false;	/* first move, then change width (when opposite border) */
                boolean ySize = false;
                for (int j = anchor.length() - 1; j >= 0; j--)
                {
                    if (anchor.charAt(j) == 'l')
                    {
                        compx = (float) x[c.x];
                        if (xSize && allowXSize) width = cellw;
                        xSize = true;
                    }
                    else if (anchor.charAt(j) == 'r')
                    {
                        if (xSize && allowXSize)
                            width = cellw;
                        else
                            compx += dw;
                        xSize = true;
                    }
                    else if (anchor.charAt(j) == 't')
                    {
                        compy = (float) y[c.y];
                        if (ySize && allowYSize) height = cellh;
                        ySize = true;
                    }
                    else if (anchor.charAt(j) == 'b')
                    {
                        if (ySize && allowYSize)
                            height = cellh;
                        else
                            compy += dh;
                        ySize = true;
                    }
                    else
                    {
                        throw new RuntimeException("Wrong character in anchor.");
                    }
                }

                comp.setBounds((int) compx + c.xCorrection, (int) compy + c.yCorrection, width, height);
            }
        }
    }

    // LayoutManager2
    /**
     * Adds the specified component to the layout, using the HIGConstraints
     * constraint object. Constraints object is copied so passed instance
     * can be safely modifed.
     * @param comp the component to be added
     * @param HIGConstraints object determining where/how the component is added to the layout.
     * @see cz.autel.dmi.HIGConstraints
     */
    public void addLayoutComponent(Component comp, Object constraints)
    {
        synchronized (comp.getTreeLock())
        {
            HIGConstraints constr = (HIGConstraints) constraints;
            if (constr.x > colCount)
            {
                throw new RuntimeException("Column index in constraint object cannot be greater then " + colCount + ".");
            }
            if (constr.x + constr.w - 1 > colCount)
            {
                throw new RuntimeException("Width in constraint object cannot be greater then "
                                           + (colCount - constr.x + 1) + ".");
            }
            if (constr.y > rowCount)
            {
                throw new RuntimeException("Row index in constraint object cannot be greater then " + rowCount + ".");
            }
            if (constr.y + constr.h - 1 > rowCount)
            {
                throw new RuntimeException("Height in constraint object cannot be greater then "
                                           + (rowCount - constr.y + 1) + ".");
            }

            /* if comp. occupies one column (row), we insert it to list for this column (row) */
            if (constr.w == 1)
            {
                if (colComponents[constr.x] == null)
                {
                    colComponents[constr.x] = new ArrayList(3);
                }
                colComponents[constr.x].add(comp);
            }
            if (constr.h == 1)
            {
                if (rowComponents[constr.y] == null)
                {
                    rowComponents[constr.y] = new ArrayList(3);
                }
                rowComponents[constr.y].add(comp);
            }
            components.put(comp, new HIGConstraints(constr));
        }

    }

    /**
     * Returns the maximum size of this component.
     * @see java.awt.Component#getMinimumSize()
     * @see java.awt.Component#getPreferredSize()
     * @see LayoutManager
     */
    public Dimension maximumLayoutSize(Container target)
    {
        synchronized (target.getTreeLock())
        {
            return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
        }
    }

    /**
     * Returns 0.
     */
    public float getLayoutAlignmentX(Container target)
    {
        return 0f;
    }

    /**
     * Returns 0.
     */
    public float getLayoutAlignmentY(Container target)
    {
        return 0f;
    }

    /**
     * Invalidates the layout, indicating that if the layout manager
     * has cached information it should be discarded.
     */
    public void invalidateLayout(Container target)
    {
        cacheColumnsX = null;
        cacheRowsY = null;
        cachePreferredLayoutSize = null;
        cacheMinimumLayoutSize = null;
    }

}

⌨️ 快捷键说明

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