XY布局JAVA

Java有什么样的XY布局吗?

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

他们每一个人都在流淌着,并且越来越好。 为了使它们小,你必须将面板放在面板中的面板中^^,

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

组件resize的原因是,无论窗口大小如何,东西看起来都很好,因此Swing不鼓励直线XY位置。 您可能希望查看专为GUI构建器设计的GroupLayout http://java.sun.com/docs/books/tutorial/uiswing/layout/group.html ,上面提到的页面描述了使用隐形组件来吸收绵延。 例如: layout.setAutoCreateGaps(true);

SpringLayout也可能有用 – 请参阅可视指南

如果您确实需要X和Y,则将布局管理器设置为null,并使用setLocation()或setBounds()。 我真的不会推荐这个,但确实有效。 阅读本教程

如果你真的想这样做,请使用

 setLayout(null); 

在您放入的组件上,然后使用

 setBounds(x, y, width, height); 

设置元素的绝对坐标。 例:

 setLayout(null); JButton myButton = new JButton("Do Stuff"); add(myButton); myButton.setBounds(30, 30, 100, 30); 

但是,生成的GUI看起来是非标准的,不会resize,如果它有任何复杂性,那将是一个难以维护的问题。

我知道布局很令人沮丧,但最终你会更好地使用BorderLayouts和FlowLayouts,orGridBagLayout – 或者像Eclipse或Netbeans这样的IDE界面构建器。

这不能回答您的具体问题,但在同意其他评论的同时,请查看MiG Layout 。 我对作为Swing新手的布局同样感到沮丧,但这已经帮了很多忙。