0
我正在使用MiGLayout,並且是我的怪胎,我在想如果可以創建佈局的單個實例並將其用於我的所有面板?只是好奇...我可以在擺動中使用單個佈局管理器實例嗎?
我正在使用MiGLayout,並且是我的怪胎,我在想如果可以創建佈局的單個實例並將其用於我的所有面板?只是好奇...我可以在擺動中使用單個佈局管理器實例嗎?
這完全取決於佈局管理器是否保留有關佈局的數據。通常,任何接受約束的佈局管理器都需要存儲這些約束,所以它必須與容器一對一地實例化。
具體參考MigLayout,看看有多少數據IT賣場,將不適用於多個容器:
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;
一些佈局管理器將只與一個Container
(如BoxLayout
)工作。我的猜測是,嘗試重複使用相同的佈局管理器並沒有多大的好處。