IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Agents de placement/Fenêtres Java Discussion :

Question CardLayout (Changement Panel)


Sujet :

Agents de placement/Fenêtres Java

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2010
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2010
    Messages : 6
    Points : 3
    Points
    3
    Par défaut Question CardLayout (Changement Panel)
    Bonjour,

    Je souhaite avoir quelques informations au sujet du CardLayout. En effet, je voudrais créer une interface graphique d'un jeu (L'interface d'entrée).

    Pour ce faire, j'ai implémenté plusieurs JPanel contenant des boutons (avec classes internes pour les listeners et actionPerformed).

    Je voudrais savoir s'il est possible de changer de panel depuis le click sur un bouton de mon JPanel ou du moins récupérer la source et invoqué la méthode show(main, "panelDemandé") pour afficher le panel que je souhaite ?

    Concrètement, mon JPanel d'entrée contient les boutons : Solo Mod, Multiplayer Mod, Option, Help, Exit. Imaginons que je click sur "Solo Mod", est-il possible de changer de panel et d'afficher celui du Solo Mod (contenant les boutons : Campaign, etc...) ?

    Au premier abord, j'avais créer à chaque fois un JFrame pour chaque panel. Mais vu qu'il y avait peu de différence entre ces Frames, je me demandais si je ne pouvais pas simplement changer mes panels.


    Je pense avoir tout expliquer correctement Merci si vous pouvez m'éclaircir

  2. #2
    Membre éclairé
    Homme Profil pro
    NoOb
    Inscrit en
    Mai 2007
    Messages
    554
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : NoOb

    Informations forums :
    Inscription : Mai 2007
    Messages : 554
    Points : 852
    Points
    852
    Par défaut
    je sais pas si c'est très propre mais il suffit de changer ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    taFenetre.setContentPane(lePanelQueTuVeux);
    Après rien ne t'empêche de faire une belle fonction qui s'en occupe proprement, avec vérification sur le panel et compagnie (que tu appellerai show(lePanelQueTuVeux))

  3. #3
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 274
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 274
    Points : 4 141
    Points
    4 141
    Par défaut
    Je suis pas certain d'avoir tout compris, mais le CardLayout a l'air adapté à ton besoin.

  4. #4
    Membre éclairé
    Homme Profil pro
    NoOb
    Inscrit en
    Mai 2007
    Messages
    554
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : NoOb

    Informations forums :
    Inscription : Mai 2007
    Messages : 554
    Points : 852
    Points
    852
    Par défaut
    Désolé j'ai du lire d'un œil ...

    J'ai trouvé ce petit exemple pour le CardLayout : http://perso.telecom-paristech.fr/~c...ng/cartes.html

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2010
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2010
    Messages : 6
    Points : 3
    Points
    3
    Par défaut
    Désolé c'est pas encore cela ^^

    Prenons l'exemple que tu m'as donné. Il y a un bouton "suivant" et "précédent". Moi j'en n'ai pas envie d'un bouton qui reste sur chaque panel. Je voudrais que quand je click sur le bouton de mon panel (celui qui est afficher) hop çà change vers un autre panel qui se trouve dans le cardLayout.

    Que je puisse mettre dans les actions de mes boutons de panel un lien vers le panel à afficher.

    Voilà, merci déjà pour les réponses.

  6. #6
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 274
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 274
    Points : 4 141
    Points
    4 141
    Par défaut
    Ce n'est qu'un exemple.
    Tu y mets bien ce que tu veux dans tes panels composants ton cardlayout !

  7. #7
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2010
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2010
    Messages : 6
    Points : 3
    Points
    3
    Par défaut
    J'ai trouvé

    Pour ceux que cela pourrait intéresser je met le code nécessaire (si jamais vous voyez quelque chose de mauvais, c'est toujours bon pour s'améliorer) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    public class CardLayoutExample
    {
        private static CardLayout cardLayout;
        public static JPanel main;
     
        public CardLayoutExample()
        {
            final JFrame frame = new JFrame ("Using a CardLayout");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     
            // Build the panels
            main = new JPanel (cardLayout = new CardLayout());
            JPanel[] panels = new JPanel[3];
     
            panels[0]= new PanelEntrance();
            panels[0].add(new JLabel("panelEntranceLabel"));
            main.add(panels[0], "panelEntrance");
     
            panels[1]= new PanelSolo();
            panels[1].add(new JLabel("panelSoloLabel"));
            main.add(panels[1], "panelSolo");
     
            panels[2]= new PanelOption();
            panels[2].add(new JLabel("panelOptionLabel"));
            main.add(panels[2], "panelOption");
     
            // Add components to the frame
            frame.add (main, BorderLayout.CENTER);
     
            // Display the frame
            frame.setSize (new Dimension (800, 600));
            frame.setResizable (false);
            frame.setVisible (true);
        }
     
        public static void changePanel(String name){
            cardLayout.show(main, name);
        }
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
     
    public class PanelEntrance extends JPanel
    {
        private ImageIcon img;
        private Button solo = new Button("Solo Mode");
        private Button multi = new Button("Multiplayer Mode");
        private Button option = new Button("Option");
        private Button help = new Button("Help");
        private Button exit = new Button("Exit");
     
        public PanelEntrance() {
     
            this.setLayout(new GridBagLayout());
     
            GridBagConstraints sol = new GridBagConstraints();
            sol.gridx = 1; sol.gridy = 1; sol.insets = new Insets(150,0,20,0); sol.ipady = 10;
            GridBagConstraints mul = new GridBagConstraints();
            mul.gridx = 1; mul.gridy = 2; mul.insets = new Insets(0,0,20,0); mul.ipady = 10;
            GridBagConstraints opt = new GridBagConstraints();
            opt.gridx = 1; opt.gridy = 3; opt.insets = new Insets(0,0,20,0); opt.ipady = 10;
            GridBagConstraints hel = new GridBagConstraints();
            hel.gridx = 1; hel.gridy = 4; hel.insets = new Insets(0,0,20,0); hel.ipady = 10;
            GridBagConstraints exi = new GridBagConstraints();
            exi.gridx = 1; exi.gridy = 5; exi.insets = new Insets(0,0,20,0); exi.ipady = 10; 
     
            solo.addActionListener(new SoloButtonListener());
     
             this.add(solo, sol);
             this.add(multi,mul);
             this.add(option,opt);
             this.add(help,hel);
             this.add(exit,exi);
        }
     
        public void paintComponent(Graphics g)
        {
            ImageIcon img = new ImageIcon("PanelBackground.gif");
            Insets insets = getInsets();
            img.paintIcon(this, g, insets.bottom, insets.left);
     
            Font font = new Font("Courier", Font.BOLD, 20);
            g.setFont(font);
            g.drawString("Gremlings War", this.getWidth()/3+this.getWidth()/14, this.getHeight()/5);
        }
     
        public void setSolo(Button solo) {
            this.solo = solo;
        }
     
        public Button getSolo() {
            return solo;
        }
     
        class SoloButtonListener implements ActionListener{
            public void actionPerformed(ActionEvent e){
                CardLayoutExample.changePanel("panelSolo");
            }
     
        }
    }
    Byeee

  8. #8
    Membre éclairé
    Homme Profil pro
    NoOb
    Inscrit en
    Mai 2007
    Messages
    554
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : NoOb

    Informations forums :
    Inscription : Mai 2007
    Messages : 554
    Points : 852
    Points
    852
    Par défaut
    Sacrée Moxxxy...

    Message inutile mais j'avais besoin de lâcher un soupir
    Comprenne qui pourra !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème de changement de Panel avec CardLayout
    Par japhettchabao dans le forum Agents de placement/Fenêtres
    Réponses: 3
    Dernier message: 02/01/2015, 09h48
  2. CardLayout + affichage panel gris
    Par krikri150489 dans le forum Interfaces Graphiques en Java
    Réponses: 8
    Dernier message: 24/05/2010, 17h54
  3. CardLayout - changement de panel
    Par jerem721 dans le forum Agents de placement/Fenêtres
    Réponses: 4
    Dernier message: 06/03/2009, 23h27
  4. [C#] [WinForms] sur [VS.NET] question sur le panel
    Par onizuanda dans le forum Windows Forms
    Réponses: 4
    Dernier message: 03/12/2004, 19h00

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo