XY布局JAVA [英] XY Layout JAVA

查看:123
本文介绍了XY布局JAVA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何类型的XY布局到Java?

is there any sort of XY-Layout to Java?

所以我可以在X和Y坐标处设置一个Button,并且假设它是那么大等....因为这个边框布局和网格和面板的东西让我发疯:)

So I can set a Button at the X and Y cordinate and that it is suppose to be that big etc.... Because this border layout and grid and panel thing is driven me crazy. :)

他们每个人都在流淌着并且得到了支持。为了使它们变小,你必须将面板放在面板中的面板中^^,

They are flowing every were and getting strecht up. And to make them small you have to put panel in panel in panel in panel ^^,

推荐答案

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

When setting the container's layout to null (no LayoutManager), you can set the component's bounds individually with component.setBounds(x,y,w,h).

固定布局在99%的情况下糟糕的用户界面设计(例如,如果你的标签在你的应用程序支持多种语言时没有达到他们的首选尺寸而遇到市长问题),那么我的建议是为您的特定需求编写专门的布局管理器

Fixed layouts are in 99% of all cases bad UI design (if your labels, for example, don't get their preferred size you run into mayor problems when your application supports multiple languages), so my advice for you is to rather write a specialized layout manager for your specific needs.

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

Writing your custom layout manager is quite easy, all you have to do is to be able to calculate the preferred size for a container with given components and your layout, and to do the layout by setting the (calculated) bounds of your components. I got rid of the GridBagLayout and started coding my own layouts long ago, and layouting has never been easier.

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

Here's an example of a custom layout, that layouts pairs of key and value components:

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<Component> 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<Component> 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<Component> 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<Component> 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<Component> {

        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或具有自定义布局的嵌套面板相比。

Just set the layout and add alternatingly labels and value components. It's easy to use, especially compared to GridBagLayout or nested panels with custom layouts.

这篇关于XY布局JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆