简体   繁体   English

秋千CardLayout

[英]Swing CardLayout

What is the default behavior of cardlayout when you add two cards with same identifier. 当您添加两个具有相同标识符的卡时,cardlayout的默认行为是什么? For example, if have panel1 which is added. 例如,如果添加了panel1 Later in the program, I add panel2 with the same string identifier. 在程序的后面,我添加panel2具有相同字符串标识符的panel2 Is the default behavior to replace panel1 with panel2 in the card stack? 用卡堆栈中的panel2替换panel1的默认行为是吗? Thanks 谢谢

Below is CardLayout's implementation of addLayoutComponent() which is executed by addLayoutComponent(Component comp, Object constraints) . 以下是CardLayout's addLayoutComponent() CardLayout's实现,该实现由addLayoutComponent(Component comp, Object constraints)

public void addLayoutComponent(String name, Component comp) {
    synchronized (comp.getTreeLock()) {
        if (!vector.isEmpty()) {
            comp.setVisible(false);
        }
        for (int i=0; i < vector.size(); i++) {
            if (((Card)vector.get(i)).name.equals(name)) {
                ((Card)vector.get(i)).comp = comp;
                return;
            }
        }
        vector.add(new Card(name, comp));
    }
}

CardLayout maintains a vector of Card objects (see below). CardLayout维护Card对象的向量(请参见下文)。 Looks like when a name collision is detected, the Component in the Card with the same name is replaced with new Component being added. 看起来在检测名称冲突时,类似ComponentCard具有相同的名称被替换为新的Component添加。 So, show() with a particular name will display the last component that was added with that name. 因此,具有特定名称的show()将显示使用该名称添加的最后一个组件。

class Card implements Serializable {
    static final long serialVersionUID = 6640330810709497518L;
    public String name;
    public Component comp;
    public Card(String cardName, Component cardComponent) {
        name = cardName;
        comp = cardComponent;
    }
}

You can add multiple cards with the same identifier. 可以添加多个具有相同标识符的卡。 You can [edit] only still [/edit] navigate to both . 仍然 只能 [/ edit]导航至 两者 show(Container, String) shows the panel added [edit] first last [/edit]. show(Container, String)显示添加的面板,[编辑] 首先是 最后 [/ edit]。

Kasper 卡斯珀

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM