Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to show different cards in a CardLayout?
    primarykey
    data
    text
    <p>I looked at a code example that used this code:</p> <pre><code>cl.show(cardPanel, "" + (currentCard)); </code></pre> <p>But when I use <code>show</code> I get a message in Eclipse that it's deprecated and I wonder if there is another way to show the different cards in the CardLayout when I click on the buttons? Below is the code for my CardLayout class. Suggestions are also welcome if some part of the code are bad practise. Thanks!</p> <pre><code>import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class CardLayoutTest extends JFrame implements ActionListener { // Ref private JPanel cardPanel, jp1, jp2, buttonPanel; private JLabel jl1, jl2; private JButton btn1, btn2; private CardLayout cardLayout; // Konstruktor public CardLayoutTest() { setTitle("Test med CardLayout"); setSize(600,400); 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"); btn2 = new JButton("Show Card 2"); buttonPanel.add(btn1); buttonPanel.add(btn2); getContentPane().add(cardPanel, BorderLayout.NORTH); getContentPane().add(buttonPanel, BorderLayout.SOUTH); btn1.addActionListener(this); } public void actionPerformed(ActionEvent event) { // ??? Show card 1 ??? // ??? Show card 2 ??? } public static void main(String[] args) { 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.
 

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