当你添加两张具有相同标识符的卡片时,卡片布局的默认行为是什么。例如,如果添加了panel1。稍后在程序中,我添加了具有相同字符串标识符的panel2。在卡片堆栈中用panel2替换panel1是默认行为吗?谢谢
发布于 2012-05-23 23:56:50
下面是由addLayoutComponent(Component comp, Object constraints)执行的addLayoutComponent()的CardLayout's实现。
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维护Card对象的向量(见下文)。当检测到名称冲突时,Card中具有相同名称的Component将替换为要添加的新Component。因此,具有特定名称的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;
}
}https://stackoverflow.com/questions/10723016
复制相似问题