jnflowlayout.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 280 行

JAVA
280
字号
/*
 * $Id: JNFlowLayout.java,v 1.2 2004/02/28 09:20:54 epr Exp $
 */
package org.jnode.wt.layouts;

import java.awt.Dimension;
import java.awt.Insets;


public class JNFlowLayout implements JNLayoutManager {
    private int hgap;
    private int vgap;

    /**
     * JNBorderLayout constructor comment.
     */
    public JNFlowLayout() {
        super();
    }

    /**
     * addLayoutComponent method comment.
     */
    public void addLayoutComponent(String name, org.jnode.wt.components.JNComponent component) {

    }

    public void layoutContainer(org.jnode.wt.components.JNSimpleContainer container) {
        int totalwidth = container.getWidth();
        //int totalheight = container.getHeight();

        Insets insets = container.getInsets();

        int cx = insets.left;
        int cy = insets.top;

        org.jnode.wt.components.JNComponent[] arr = container.getComponents();
//        JNComponent[] arr = new JNComponent[ container.getComponentsCount() ];

        int endpointx = 0;
        int maxheight_in_thisline = 0;
        // to find the widest component of all.
        for (int i = 0; i < arr.length; i++) {
            org.jnode.wt.components.JNComponent component = arr[i];

            if (component.isVisible()) {
                Dimension psize = component.getPreferredSize();
                component.setSize(psize);
                endpointx = cx + psize.width + hgap;

                int temp = cy + (int) psize.getHeight() + vgap;
                maxheight_in_thisline = Math.max(maxheight_in_thisline, temp);

                if (endpointx > totalwidth) // Move to next line.
                {
                    cx = insets.left;
                    //cy = cy + (int)psize.getHeight() + vgap;
                    cy = maxheight_in_thisline;
                }
                /* else {
                     cx = cx + insets.left;
                 }*/

                component.setLocation(cx, cy);
                cx = endpointx;

//                 System.out.println("  "+cx+"  "+cy);
            }

        }
    }


    /*
    public void layoutContainer(org.jnode.wt.components.JNSimpleContainer container) {
        int totalwidth = container.getWidth();
        Insets insets = container.getInsets();

        int cx = insets.left;
        int cy = insets.top;

        // I need to check why the following method is notworking.
        //        JNComponent[] arr = container.getComponents();
        JNComponent[] arr = new JNComponent[container.getComponentCount()];

        int j = 0;
        for (Iterator it = container.getComponentList().iterator(); it.hasNext();) {
            arr[j] = (JNComponent) it.next();
            j++;
        }

        int endpointx = 0;
        int maxheight_in_thisline = 0;
        // to find the widest component of all.
        for (int i = 0; i < arr.length; i++) {
            JNComponent component = arr[i];

            if (component.isVisible()) {
                Dimension psize = component.getPreferredSize();
                component.setSize(psize);
                endpointx = cx + psize.width + hgap;

                int temp = cy + (int) psize.getHeight() + vgap;
                maxheight_in_thisline = Math.max(maxheight_in_thisline, temp);

                if (endpointx > totalwidth) // Move to next line.
                {
                    cx = insets.left;
                    //cy = cy + (int)psize.getHeight() + vgap;
                    cy = maxheight_in_thisline;
                } else {
                    cx = cx + insets.left;
                }

                component.setLocation(cx, cy);
                cx = endpointx;

                //                 System.out.println(" "+cx+" "+cy);
            }

        }
    } */

/*
 * TODO: This method currently returns max width of this container as width
 *       and calculates the height of total compoents.
 */
    public java.awt.Dimension minimumLayoutSize(org.jnode.wt.components.JNSimpleContainer container) {

        org.jnode.wt.components.JNComponent[] arr = container.getComponents();

        if ((arr == null) && (arr.length <= 0)) {
            return new Dimension(0, 0);
        }

        int maxwidth = 0;
        int totalheight = 0;

        for (int i = 0; i < arr.length; i++) {
            org.jnode.wt.components.JNComponent component = arr[i];

            if (component.isVisible()) {
                Dimension psize = component.getPreferredSize();
                if (psize.getWidth() > maxwidth) {
                    maxwidth = (int) psize.getWidth();
                }

                totalheight = totalheight + (int) psize.getHeight();
            }
        }


        maxwidth = maxwidth + (2 * hgap);
        totalheight = totalheight + (2 * vgap);

        return new Dimension(maxwidth, totalheight);
    }
/*
 * TODO: I need to recheck.
 */
    public Dimension preferredLayoutSize(org.jnode.wt.components.JNSimpleContainer container) {
        int totalwidth = container.getWidth();
        //int totalheight = container.getHeight();

        Insets insets = container.getInsets();

        int cx = insets.left;
//	int cy = insets.top;
        int cy = 0;

        org.jnode.wt.components.JNComponent[] arr = container.getComponents();


        if ((arr == null) && (arr.length <= 0)) {
            return new Dimension(0, 0);
        }

        int endpointx = 0;
        int maxheight_in_thisline = 0;
        //
        boolean increaseHeight = true;
        boolean ismaxwidth = false;
        for (int i = 0; i < arr.length; i++) {
            org.jnode.wt.components.JNComponent component = arr[i];

            if (component.isVisible()) {
                Dimension psize = component.getPreferredSize();
                endpointx = cx + psize.width + hgap;

                int temp = cy + (int) psize.getHeight() + vgap;
                maxheight_in_thisline = Math.max(maxheight_in_thisline, temp);


                if (increaseHeight) {
                    cy = cy + maxheight_in_thisline;
                    increaseHeight = false;
                }

                if (endpointx > totalwidth) // Move to next line.
                {
                    cx = insets.left;
                    //cy = cy + (int)psize.getHeight() + vgap;
                    increaseHeight = true;
                    ismaxwidth = true;
                } else {
                    cx = cx + insets.left;
                }
                cx = endpointx;
//                 System.out.println("  "+cx+"  "+cy);
            }
        }

        if (ismaxwidth) {
            cx = totalwidth;
        }

        cy = cy + insets.top + insets.bottom;
        return new Dimension(cx, cy);
    }


    /*
    public Dimension preferredLayoutSize(org.jnode.wt.components.JNSimpleContainer container) {
        Dimension dimension = new Dimension(0, 0);

        // to find the widest component of all.

        for (Iterator it = container.getComponentList().iterator(); it.hasNext();) {
            JNComponent component = (JNComponent) it.next();

            if (component.isVisible()) {
                Dimension dimension1 = component.getPreferredSize();
                dimension.height = Math.max(dimension.height, dimension1.height);
                dimension.width += hgap;
                dimension.width += dimension1.width;
            }
        }

        Insets insets = container.getInsets();
        dimension.width += insets.left + insets.right + hgap * 2;
        dimension.height += insets.top + insets.bottom + vgap * 2;
        return dimension;
    } */


    public void removeLayoutComponent(org.jnode.wt.components.JNComponent component) {

    }

/*
 * TODO: I need to check this, this code i not working correctly,
 */

    public Dimension preferredLayoutSize2(org.jnode.wt.components.JNSimpleContainer container) {
        Dimension dimension = new Dimension(0, 0);

        org.jnode.wt.components.JNComponent[] arr = container.getComponents();

        // to find the widest component of all.
        for (int i = 0; i < arr.length; i++) {
            org.jnode.wt.components.JNComponent component = arr[i];

            if (component.isVisible()) {
                Dimension compDimension = component.getPreferredSize();

                dimension.height = Math.max(dimension.height, compDimension.height);
                if (i > 0)
                    dimension.width += hgap;
                dimension.width += compDimension.width;
            }
        }

        Insets insets = container.getInsets();
        dimension.width += insets.left + insets.right + hgap * 2;
        dimension.height += insets.top + insets.bottom + vgap * 2;

        return dimension;
    }
}

⌨️ 快捷键说明

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