2012-05-30 11 views
6

Ho guardato un esempio di codice che ha utilizzato questo codice:Come mostrare carte diverse in un CardLayout?

cl.show(cardPanel, "" + (currentCard)); 

Ma quando uso show ricevo un messaggio in Eclipse che è deprecato e mi chiedo se c'è un altro modo per mostrare le diverse carte del CardLayout quando clicco sui pulsanti? Di seguito è riportato il codice per la mia classe CardLayout. I suggerimenti sono anche benvenuti se alcune parti del codice sono una cattiva pratica. Grazie!

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

risposta

13

EDIT (ho cercato il tuo esempio di codice)

1.you dimenticato di inizializzare la variabile più importante

private CardLayout cardLayout = new CardLayout(); 

2.Then SSCCE potrebbe essere

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); 
      } 
     }); 
    } 
} 
+0

Grazie per il nuovo codice! Funziona bene! Ma ho alcune domande su alcune linee che hai aggiunto, come questa: private static final long serialVersionUID = 1L; Vedo che ho rimosso un triangolo di avviso giallo in Eclipse, ma cosa sta facendo? E hai anche sostituito questa riga: getContentPane(). Add (cardPanel, BorderLayout.NORTH); Con questo: aggiungi (cardPanel, BorderLayout.NORTH); qual è la differenza? E infine potresti anche essere gentile e spiegare cosa hai fatto nel metodo principale? Grazie! –

8

E 'più probabile che si sta chiamando show() sul JPanel, piuttosto che il CardLayout.

Il metodo show() utilizzato per esistere in JPanel (più specificamente Component) è stato sostituito da setVisible(). Questo è completamente diverso dal metodo show() di CardLayout.

Assicurarsi che si sta facendo qualcosa di simile a quanto segue nella vostra azione ascoltatore

CardLayout cardLayout = (CardLayout)(cardPanel.getLayout()); 
cardLayout.show(cardPanel, "CardToShow"); 

Come nota a margine, si pretende molto apparire sei "Newing" il tuo CardLayout. Assicurati di farlo.

+0

Sì, mi mancava "newing" il mio CardLayout e ho anche fatto un po 'di misstake con lo show! Grazie! –

Problemi correlati