CardLayout 语法 // .add()

CardLayout syntax // .add()

来自以下我从 CardLayoutDemo 中获取的代码片段。

final static String BUTTONPANEL = "Card with JButtons";
final static String TEXTPANEL = "Card with JTextField";

-

//Create the panel that contains the "cards".
  cards = new JPanel(new CardLayout());
  cards.add(card1, BUTTONPANEL);
  cards.add(card2, TEXTPANEL);

我不明白上面2个字符串的用法。我假设它们是特定卡片的指示符,但为什么我们不使用以下代码呢?

//Create the panel that contains the "cards".
      cards = new JPanel(new CardLayout());
      cards.add(buttonPanel);
      cards.add(textPanel);

我的意思是这 2 个字符串根本没有实际用途,这对我来说很奇怪。

对不起我的菜鸟,如果问题含糊不清,请原谅我。

String "name" 是一个标识符,它允许您指示 CardLayout 它应该显示哪个视图,这在您发布在 How to Use CardLayout...

//Method came from the ItemListener class implementation,
//contains functionality to process the combo box item selecting
public void itemStateChanged(ItemEvent evt) {
    CardLayout cl = (CardLayout)(cards.getLayout());
    cl.show(cards, (String)evt.getItem());
}