gpt4 book ai didi

java - XY 布局 JAVA

转载 作者:行者123 更新时间:2023-12-02 05:56:05 26 4
gpt4 key购买 nike

Java 有 XY 布局吗?

所以我可以在 X 和 Y 坐标处设置一个 Button,它应该有那么大等等....因为这个边框布局、网格和面板让我发疯。 :)

它们每时每刻都在流动,并且越来越高。为了使它们变小,您必须将面板放在面板中的面板中 ^^,

最佳答案

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

关于java - XY 布局 JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/519918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com