Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ul> <li><p>I can't see that <a href="http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html#show%28java.awt.Container,%20java.lang.String%29">Java7 show(Container parent, String name)</a> or <a href="http://docs.oracle.com/javase/6/docs/api/java/awt/CardLayout.html#show%28java.awt.Container,%20java.lang.String%29">Java6 show(Container parent, String name)</a> is depreciated</p></li> <li><p>depends if <code>currentCard</code> returns <code>String</code> from <code>cl.show(cardPanel, "" + (currentCard));</code></p></li> </ul> <p>EDIT (I tried your code example)</p> <p>1.you forgot to initialize most important variable </p> <pre><code>private CardLayout cardLayout = new CardLayout(); </code></pre> <p>2.then SSCCE could be </p> <pre><code>import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class CardLayoutTest extends JFrame { private static final long serialVersionUID = 1L; private JPanel cardPanel, jp1, jp2, buttonPanel; private JLabel jl1, jl2; private JButton btn1, btn2; private CardLayout cardLayout = new CardLayout(); public CardLayoutTest() { setTitle("Test med CardLayout"); setSize(400, 300); cardPanel = new JPanel(); buttonPanel = new JPanel(); cardPanel.setLayout(cardLayout); jp1 = new JPanel(); jp2 = new JPanel(); jl1 = new JLabel("Card 1"); jl2 = new JLabel("Card 2"); jp1.add(jl1); jp2.add(jl2); cardPanel.add(jp1, "1"); cardPanel.add(jp2, "2"); btn1 = new JButton("Show Card 1"); btn1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { cardLayout.show(cardPanel, "1"); } }); btn2 = new JButton("Show Card 2"); btn2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { cardLayout.show(cardPanel, "2"); } }); buttonPanel.add(btn1); buttonPanel.add(btn2); add(cardPanel, BorderLayout.NORTH); add(buttonPanel, BorderLayout.SOUTH); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { CardLayoutTest frame = new CardLayoutTest(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload