我可以在 swing 中使用单个布局管理器实例吗?

发布于 2024-08-26 01:11:10 字数 65 浏览 5 评论 0原文

我正在使用 MiGLayout 并且是个怪人,我在想是否可以创建布局的单个实例并将其用于我的所有面板?只是好奇...

I'm using MiGLayout and being the freak i am, i was thinking if it's possible to create a single instance of the layout and use it for all my panels? Just curious...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

予囚 2024-09-02 01:11:10

这完全取决于布局管理器是否保留与布局相关的数据。一般来说,任何接受约束的布局管理器都需要存储这些约束,因此它必须与容器一对一地实例化。

特别参考 MigLayout,看看它存储了多少不适用于多个容器的数据:

public final class MigLayout implements LayoutManager2, Externalizable
{
    // ******** Instance part ********

    /** The component to string constraints mappings.
     */
    private final Map<Component, Object> scrConstrMap = new IdentityHashMap<Component, Object>(8);

    /** Hold the serializable text representation of the constraints.
     */
    private Object layoutConstraints = "", colConstraints = "", rowConstraints = "";    // Should never be null!

    // ******** Transient part ********

    private transient ContainerWrapper cacheParentW = null;

    private transient final Map<ComponentWrapper, CC> ccMap = new HashMap<ComponentWrapper, CC>(8);
    private transient javax.swing.Timer debugTimer = null;

    private transient LC lc = null;
    private transient AC colSpecs = null, rowSpecs = null;
    private transient Grid grid = null;
    private transient int lastModCount = PlatformDefaults.getModCount();
    private transient int lastHash = -1;
    private transient Dimension lastInvalidSize = null;

    private transient ArrayList<LayoutCallback> callbackList = null;

    private transient boolean dirty = true;

That depends entirely on whether the layout manager retains data related to layout. Generally, any layout manager that takes constraints needs to store those, so it must be instantiated one-to-one with the container.

With particular reference to MigLayout, look at how much data it stores that would not be applicable to multiple containers:

public final class MigLayout implements LayoutManager2, Externalizable
{
    // ******** Instance part ********

    /** The component to string constraints mappings.
     */
    private final Map<Component, Object> scrConstrMap = new IdentityHashMap<Component, Object>(8);

    /** Hold the serializable text representation of the constraints.
     */
    private Object layoutConstraints = "", colConstraints = "", rowConstraints = "";    // Should never be null!

    // ******** Transient part ********

    private transient ContainerWrapper cacheParentW = null;

    private transient final Map<ComponentWrapper, CC> ccMap = new HashMap<ComponentWrapper, CC>(8);
    private transient javax.swing.Timer debugTimer = null;

    private transient LC lc = null;
    private transient AC colSpecs = null, rowSpecs = null;
    private transient Grid grid = null;
    private transient int lastModCount = PlatformDefaults.getModCount();
    private transient int lastHash = -1;
    private transient Dimension lastInvalidSize = null;

    private transient ArrayList<LayoutCallback> callbackList = null;

    private transient boolean dirty = true;
三生一梦 2024-09-02 01:11:10

某些布局管理器仅适用于一个 Container(例如 BoxLayout)。我的猜测是,尝试重用相同的布局管理器几乎没有什么好处。

Some layout managers will only work with one Container (such as BoxLayout). My guess is that there's little benefit gained by trying to reuse the same layout manager.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文