当前位置:  开发笔记 > 编程语言 > 正文

XY布局JAVA

如何解决《XY布局JAVA》经验,为你挑选了1个好方法。

Java有哪种XY布局?

所以我可以设置一个按钮在X和Y坐标,它被认为是那么大等....因为这个边框布局和网格和面板的事情让我疯了.:)

他们每一个人都在流动,并且得到了拉伸.为了使它们变小,你必须将面板放在面板中的面板中^^,



1> Peter Walser..:

将容器的布局设置为null(无LayoutManager)时,可以使用component.setBounds(x,y,w,h)单独设置组件的边界.

固定布局在99%的情况下都是糟糕的UI设计(例如,如果您的标签没有得到他们的首选尺寸,当您的应用程序支持多种语言时会遇到市长问题),所以我的建议是写一个为您的特定需求专门的布局管理器.

编写自定义布局管理器非常简单,您所要做的就是能够计算具有给定组件和布局的容器的首选大小,并通过设置组件的(计算)边界来进行布局.我摆脱了GridBagLayout并且很久以前开始编写我自己的布局,布局从未如此简单.

这是一个自定义布局的示例,它布局键和值组件对:

public class KeyValueLayout implements LayoutManager {

    public static enum KeyAlignment {
        LEFT, RIGHT;
    }

    private KeyAlignment keyAlignment = KeyAlignment.LEFT;

    private int hgap;

    private int vgap;

    public KeyValueLayout () {
        this(KeyAlignment.LEFT);
    }

    public KeyValueLayout (KeyAlignment keyAlignment) {
        this(keyAlignment, 5, 5);
    }

    public KeyValueLayout (int hgap, int vgap) {
        this(KeyAlignment.LEFT, hgap, vgap);
    }

    public KeyValueLayout (KeyAlignment keyAlignment, int hgap, int vgap) {

        this.keyAlignment = keyAlignment != null ? keyAlignment : KeyAlignment.LEFT;
        this.hgap = hgap;
        this.vgap = vgap;
    }

    public void addLayoutComponent (String name, Component comp) {
    }

    public void addLayoutComponent (Component comp, Object constraints) {
    }

    public void removeLayoutComponent (Component comp) {
    }

    public void layoutContainer (Container parent) {
        Rectangle canvas = getLayoutCanvas(parent);
        int ypos = canvas.y;
        int preferredKeyWidth = getPreferredKeyWidth(parent);

        for (Iterator iter = new ComponentIterator(parent); iter.hasNext();) {
            Component key = (Component) iter.next();
            Component value = iter.hasNext() ? (Component) iter.next() : null;
            int xpos = canvas.x;
            int preferredHeight = Math.max(key.getPreferredSize().height, value != null ? value.getPreferredSize().height : 0);

            if (keyAlignment == KeyAlignment.LEFT)
                key.setBounds(xpos, ypos, key.getPreferredSize().width, key.getPreferredSize().height);
            else
                key.setBounds(xpos + preferredKeyWidth - key.getPreferredSize().width, ypos, key.getPreferredSize().width,
                    key.getPreferredSize().height);

            xpos += preferredKeyWidth + hgap;
            if (value != null)
                value.setBounds(xpos, ypos, canvas.x + canvas.width - xpos, preferredHeight);
            ypos += preferredHeight + vgap;
        }
    }

    public Dimension minimumLayoutSize (Container parent) {
        int preferredKeyWidth = getPreferredKeyWidth(parent);
        int minimumValueWidth = 0;
        int minimumHeight = 0;
        int lines = 0;
        for (Iterator iter = new ComponentIterator(parent); iter.hasNext();) {
            lines++;
            Component key = (Component) iter.next();
            Component value = iter.hasNext() ? (Component) iter.next() : null;
            minimumHeight += Math.max(key.getPreferredSize().height, value != null ? value.getMinimumSize().height : 0);
            minimumValueWidth = Math.max(minimumValueWidth, value != null ? value.getMinimumSize().width : 0);
        }

        Insets insets = parent.getInsets();
        int minimumWidth = insets.left + preferredKeyWidth + hgap + minimumValueWidth + insets.right;
        minimumHeight += insets.top + insets.bottom;
        if (lines > 0)
            minimumHeight += (lines - 1) * vgap;

        return new Dimension(minimumWidth, minimumHeight);
    }

    public Dimension preferredLayoutSize (Container parent) {
        int preferredKeyWidth = getPreferredKeyWidth(parent);
        int preferredValueWidth = 0;
        int preferredHeight = 0;
        int lines = 0;
        for (Iterator iter = new ComponentIterator(parent); iter.hasNext();) {
            lines++;
            Component key = (Component) iter.next();
            Component value = iter.hasNext() ? (Component) iter.next() : null;

            preferredHeight += Math.max(key.getPreferredSize().height, value != null ? value.getPreferredSize().height : 0);
            preferredValueWidth = Math.max(preferredValueWidth, value != null ? value.getPreferredSize().width : 0);
        }

        Insets insets = parent.getInsets();
        int preferredWidth = insets.left + preferredKeyWidth + hgap + preferredValueWidth + insets.right;
        preferredHeight += insets.top + insets.bottom;
        if (lines > 0)
            preferredHeight += (lines - 1) * vgap;

        return new Dimension(preferredWidth, preferredHeight);
    }

    public Dimension maximumLayoutSize (Container target) {
        return preferredLayoutSize(target);
    }

    private int getPreferredKeyWidth (Container parent) {
        int preferredWidth = 0;
        for (Iterator iter = new ComponentIterator(parent); iter.hasNext();) {
            Component key = (Component) iter.next();
            if (iter.hasNext())
                iter.next();

            preferredWidth = Math.max(preferredWidth, key.getPreferredSize().width);
        }

        return preferredWidth;
    }

    private Rectangle getLayoutCanvas (Container parent) {
        Insets insets = parent.getInsets();
        int x = insets.left;
        int y = insets.top;

        int width = parent.getSize().width - insets.left - insets.right;
        int height = parent.getSize().height - insets.top - insets.bottom;

        return new Rectangle(x, y, width, height);
    }

    private class ComponentIterator implements Iterator {

        private Container container;

        private int index = 0;

        public ComponentIterator (Container container) {
            this.container = container;
        }

        public boolean hasNext () {
            return index < container.getComponentCount();
        }

        public Component next () {
            return container.getComponent(index++);
        }

        public void remove () {
        }
    }
}

只需设置布局并交替添加标签和值组件.它易于使用,特别是与GridBagLayout或具有自定义布局的嵌套面板相比.

推荐阅读
手机用户2502851955
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有