【问题标题】:CardLayout syntax // .add()CardLayout 语法 // .add()
【发布时间】:2015-04-24 01:46:56
【问题描述】:

来自我从CardLayoutDemo获取的以下代码sn-p。

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 个字符串根本没有实际用途,这对我来说似乎很奇怪。

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

【问题讨论】:

    标签: java awt cardlayout


    【解决方案1】:

    String“名称”是一个标识符,它允许您指示CardLayout 它应该显示哪个视图,这在您在How to Use CardLayout 中发布的代码 sn-p 下方进行了描述...

    //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());
    }
    

    【讨论】:

    • 是的,我完全错过了。谢谢。我想我需要更多地适应面向对象的概念。
    猜你喜欢
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多