对齐不同DesignGridLayout的标签列 [英] Align the label column of different DesignGridLayout

查看:137
本文介绍了对齐不同DesignGridLayout的标签列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 DesignGridLayout 来考虑两个JPanel实例,如下所示:

Consider two JPanel instances using DesignGridLayout, like the following:

JFrame demoApplicationFrame = new JFrame();
JPanel singleDesignGridLayout1 = new JPanel();
DesignGridLayout layout1 = new DesignGridLayout(singleDesignGridLayout1);
layout1.row().grid(new JLabel("Short lbl1")).add(new JTextField(""));
layout1.row().grid(new JLabel("Short lbl2")).add(new JTextField(""));

JPanel singleDesignGridLayout2 = new JPanel();
DesignGridLayout layout2 = new DesignGridLayout(singleDesignGridLayout2);
layout2.row().grid(new JLabel("A bit longer label"))
    .add(new JTextField(""));
layout2.row().grid(new JLabel("Another long label here"))
    .add(new JTextField(""));

demoApplicationFrame.getContentPane().add(singleDesignGridLayout1,
     BorderLayout.NORTH);
demoApplicationFrame.getContentPane().add(singleDesignGridLayout2,
    BorderLayout.SOUTH);

执行该代码,将创建此窗口:

Executing that code, will create this window:

是否可以使两个面板的标签的列对齐?

Is it possible to align the column for labels of the two panels?

推荐答案

是否可以使两个面板的标签的列对齐?

Is it possible to align the column for labels of the two panels?

不,这不是因为 DesignGridLayout 可以在 内部 相同的容器.因此,如果将组件放置在不同的面板中,则DesignGridLayout不能确保跨面板组件的对齐.

No it's not because DesignGridLayout works with grids within the same container. So if your components are placed in different panels there's nothing DesignGridLayout can do to ensure the alignment of cross-panels components.

在这种情况下,您只能寻找解决方法(如果有).请注意:我应该避免使用Java Swing中的set(Preferred | Maximum | Minimum)Size方法的设置?是的,我们应该这样做.因此,设置固定标签大小也不是一种选择.

In this context you can only look for workarounds (if any). Be aware of this: Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? Yes, we should. So setting a fixed labels size is not an option either.

我正在考虑这个问题,这不是DesignGridLayout 限制,而是关于布局管理器的工作方式.如果您查看布局管理器的工作原理部分, 在容器中布置组件课程,它指出:

I was thinking about this matter and it's not a DesignGridLayout limitation but this is about how layout managers work. If you take a look to How Layout Managers work section of Laying Out Components Within a Container lesson, it states this:

  1. 布局管理器基本上要做两件事:

  1. Layout managers basically do two things:

  • 计算容器的最小/首选/最大尺寸.
  • 布置集装箱的孩子.

布局管理器会根据提供的约束来执行此操作, 容器的属性(例如插图)和子项的属性 最小/首选/最大尺寸.如果孩子本身就是一个容器,那么 它自己的布局管理器用于获取其最小/首选/最大 尺寸并进行布局.

Layout managers do this based on the provided constraints, the container's properties (such as insets) and on the children's minimum/preferred/maximum sizes. If a child is itself a container then its own layout manger is used to get its minimum/preferred/maximum sizes and to lay it out.

这意味着,如果子代本身是一个容器,则它可能具有自己的布局管理器来布局其自己的子代组件.此属性使此容器独立于其祖先(关于布局组件).如果您有一个全局"(以某种方式命名)布局管理器,它在两个单独的容器中对齐 内的组件,那么它将违反上述约定,因为子容器不会不能独立于他们的祖先.

This means if a child itself is a container then it may have its own layout manager to lay out its own children components. This property makes this container independent of its ancestor (about laying out components). If you had a "global" (just to name it somehow) layout manager that aligns components within two separate containers, then it would be breaking the aforementioned contract because children containers wouldn't be independent of their ancestors.

我对这种布局管理器还比较陌生,但是我不知道任何允许您执行所需(或您认为自己需要)的布局管理器,因为实际上他们并不需要.恕我直言,如果您确实需要所有这些面板,或者您可以在一个容器中管理所有组件,则应该重新考虑.在此列表中,也许有一个更合适的布局管理器可以满足您的需求:

I'm relatively new to this layout managers stuff but I don't know any layout manager that allows you do what you need (or you feel you need), because they don't have to, actually. IMHO you should reconsider if you really need all those panels or you can manage all your components in a single container. Maybe there's a more suitable layout manager to your needs in this list:

  • MiG Layout
  • FormLayout
  • GridBagLayout (not my favorite at all but it might work)

这篇关于对齐不同DesignGridLayout的标签列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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