CardLayout语法// .add()

时间:2015-04-24 01:46:56

标签: java awt cardlayout

来自我从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弦完全没有用,这对我来说似乎很奇怪。

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

1 个答案:

答案 0 :(得分:1)

String"名称"是一个标识符,可让您指示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());
}