Advertisement
DulcetAirman

fen1

Feb 23rd, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.CardLayout;
  3. import java.awt.Color;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9.  
  10. public class fen1 extends JFrame {
  11.  
  12.     CardLayout cl = new CardLayout();
  13.     JPanel content = new JPanel();
  14.     // Liste des noms de nos conteneurs pour la pile de cartes
  15.     String[] listContent = { "CARD_1", "CARD_2", "CARD_3" };
  16.     int indice = 0;
  17.  
  18.     public fen1() {
  19.         this.setTitle("CardLayout");
  20.         this.setSize(300, 120);
  21.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.         this.setLocationRelativeTo(null);
  23.  
  24.         // On crée trois conteneurs de couleur différente
  25.         JPanel card1 = new JPanel();
  26.         card1.setBackground(Color.blue);
  27.         JPanel card2 = new JPanel();
  28.         card2.setBackground(Color.red);
  29.         JPanel card3 = new JPanel();
  30.         card3.setBackground(Color.green);
  31.  
  32.         JPanel boutonPane = new JPanel();
  33.         JButton bouton = new JButton("Contenu suivant");
  34.         // Définition de l'action du bouton
  35.         bouton.addActionListener(new ActionListener() {
  36.             public void actionPerformed(ActionEvent event) {
  37.                 // Via cette instruction, on passe au prochain conteneur de la pile
  38.                 cl.next(content);
  39.             }
  40.         });
  41.  
  42.         JButton bouton2 = new JButton("Contenu par indice");
  43.         // Définition de l'action du bouton2
  44.         bouton2.addActionListener(new ActionListener() {
  45.             public void actionPerformed(ActionEvent event) {
  46.                 if (++indice > 2)
  47.                     indice = 0;
  48.                 // Via cette instruction, on passe au conteneur correspondant au nom fourni en
  49.                 // paramètre
  50.                 cl.show(content, listContent[indice]);
  51.             }
  52.         });
  53.  
  54.         boutonPane.add(bouton);
  55.         boutonPane.add(bouton2);
  56.         // On définit le layout
  57.         content.setLayout(cl);
  58.         // On ajoute les cartes à la pile avec un nom pour les retrouver
  59.         content.add(card1, listContent[0]);
  60.         content.add(card2, listContent[1]);
  61.         content.add(card3, listContent[2]);
  62.  
  63.         this.getContentPane().add(boutonPane, BorderLayout.NORTH);
  64.         this.getContentPane().add(content, BorderLayout.CENTER);
  65.         this.setVisible(true);
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement