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

时间:2010-03-19 17:19:56

标签: java swing miglayout

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

2 个答案:

答案 0 :(得分:2)

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

特别参考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;

答案 1 :(得分:0)

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