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

Interfaces Graphiques en Java Discussion :

Comment utiliser le CardLayout


Sujet :

Interfaces Graphiques en Java

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2012
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2012
    Messages : 4
    Points : 5
    Points
    5
    Par défaut Comment utiliser le CardLayout
    Bonjour !

    Je travaille actuellement sur un projet en Java : je créer un petit programme avec générateurs de nombres aléatoires, c'est pour mon travail de maturité.

    Je crée une fenêtre, et dans la première partie se trouve une liste de RadioButton : c'est la "méthode" à choisir pour la génération des nombres.

    Puis dans la deuxième partie, j'aimerais que un/plusieurs champs soient affichés
    Ex : l'utilisateur choisit la méthode "1", alors les champs spécifiques à la méthode "1" s'affichent... etc...
    C'est pourquoi j'ai utilisé le CardLayout.

    Voici les JPanels à afficher en fonction des choix de l'utilisateur :

    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
    // JPanel du formulaire des coefficients : Panel2
    		Panel2.setPreferredSize(new Dimension(150, 250));
    		Panel2.setBackground(Color.GREEN);
    		Panel2.setLayout(pile);
    		container.add(Panel2, BorderLayout.CENTER);
    		// JPanel pour le choix "Carrées médians" : Panel2_0
    		Panel2_0.add(germe1);
    		Panel2.add(Panel2_0, "2_0");
    		// JPanel pour le choix "Fibonacci" : Panel2_1
    		Panel2_1.add(germe1);
    		Panel2_1.add(germe2);
    		Panel2_1.add(modulo);
    		Panel2.add(Panel2_1, "2_1");
    		// JPanel pour le choix "GCL" : Panel2_2
    		Panel2_2.add(germe1);
    		Panel2_2.add(multiplicateur);
    		Panel2_2.add(incrément);
    		Panel2_2.add(modulo);
    		Panel2.add(Panel2_2, "2_2");
    		// JPanel pour le choix "LFSR" : Panel2_3
    		Panel2_3.add(germe1);
    		Panel2.add(Panel2_3, "2_3");
    Et voici le listener des RadioButtons qui devrait afficher les JPanels ci-dessus respectifs aux choix de l'utilisateur :
    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
     
    class ChoiceListener implements ActionListener {
    		// Listener des RadioButton (choix de la méthode)
    		public void actionPerformed(ActionEvent e) {
    			if (bg1.getSelection().getMnemonic() == 0) {
    				pile.show(Panel2, "2_0");
    			}
    			if (bg1.getSelection().getMnemonic() == 1) {
    				pile.show(Panel2, "2_1");
    			}
    			if (bg1.getSelection().getMnemonic() == 2) {
    				pile.show(Panel2, "2_2");
    			}
    			if (bg1.getSelection().getMnemonic() == 3) {
    				pile.show(Panel2, "2_3");
     
    			}
    		}

    --> Le problème est que rien ne se passe quand je clique sur un RadioButton, si ce n'est plein d'exceptions

    Merci de m'aiguiller, m'aider, suggérer, etc...
    Je suis encore débutant, soyez svp indulgent

    Le code complet :
    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
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.PrintStream;
    import java.text.NumberFormat;
     
    import javax.swing.JFormattedTextField;
    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JRadioButton;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
     
    public class Fenetre extends JFrame implements ActionListener {
    	/**
             * 
             */
    	private static final long serialVersionUID = 1L;
     
    	private CardLayout pile;
     
    	// JPannels
    	private JPanel container = new JPanel();
    	private JPanel Panel1 = new JPanel();
    	private JPanel Panel2 = new JPanel();
    	private JPanel Panel2_0 = new JPanel();
    	private JPanel Panel2_1 = new JPanel();
    	private JPanel Panel2_2 = new JPanel();
    	private JPanel Panel2_3 = new JPanel();
     
    	// JTextArea
    	private JTextArea results = new JTextArea();
     
    	// RadioButtons (choix de la méthode)
    	private JRadioButton choice1 = new JRadioButton("Carrés médians");
    	private JRadioButton choice2 = new JRadioButton("Fibonacci");
    	private JRadioButton choice3 = new JRadioButton("GCL");
    	private JRadioButton choice4 = new JRadioButton("LFSR");
    	// ButtonGroup associé au RadioButtons
    	private ButtonGroup bg1 = new ButtonGroup();
     
    	// Button normal
    	private JButton test = new JButton("Test");
     
    	// JFormattedTextField (champs du formulaire des coefficients)
    	private JFormattedTextField germe1 = new JFormattedTextField(
    			NumberFormat.getIntegerInstance());
    	private JFormattedTextField germe2 = new JFormattedTextField(
    			NumberFormat.getIntegerInstance());
    	private JFormattedTextField multiplicateur = new JFormattedTextField(
    			NumberFormat.getIntegerInstance());
    	private JFormattedTextField incrément = new JFormattedTextField(
    			NumberFormat.getIntegerInstance());
    	private JFormattedTextField modulo = new JFormattedTextField(
    			NumberFormat.getIntegerInstance());
     
    	// JLabel
    	private JLabel label = new JLabel("0");
     
    	public Fenetre() {
     
    		this.setTitle("Générateur de nombres pseudo aléatoires");
    		this.setSize(900, 600);
    		this.setLocationRelativeTo(null);
    		this.setResizable(false);
    		this.setVisible(true);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		// ContentPane : container
    		container.setBackground(Color.ORANGE);
    		container.setLayout(new BorderLayout());
    		this.setContentPane(container);
     
    		// JPanel des choix de l'utilisateur (méthode) : Panel1
    		Panel1.setLayout(new GridLayout(4, 1));
    		Panel1.setPreferredSize(new Dimension(150, 500));
    		// Panel1.setLocation(0, 0);
    		container.add(Panel1, BorderLayout.WEST);
    		//
    		choice1.addActionListener(new ChoiceListener());
    		choice2.addActionListener(new ChoiceListener());
    		choice3.addActionListener(new ChoiceListener());
    		choice4.addActionListener(new ChoiceListener());
    		//
    		choice1.setMnemonic(0);
    		choice2.setMnemonic(1);
    		choice3.setMnemonic(2);
    		choice4.setMnemonic(3);
    		//
    		Panel1.add(choice1);
    		Panel1.add(choice2);
    		Panel1.add(choice3);
    		Panel1.add(choice4);
    		//
    		bg1.add(choice1);
    		bg1.add(choice2);
    		bg1.add(choice3);
    		bg1.add(choice4);
     
    		// JPanel du formulaire des coefficients : Panel2
    		Panel2.setPreferredSize(new Dimension(150, 250));
    		Panel2.setBackground(Color.GREEN);
    		Panel2.setLayout(pile);
    		container.add(Panel2, BorderLayout.CENTER);
    		// JPanel pour le choix "Carrées médians" : Panel2_0
    		Panel2_0.add(germe1);
    		Panel2.add(Panel2_0, "2_0");
    		// JPanel pour le choix "Fibonacci" : Panel2_1
    		Panel2_1.add(germe1);
    		Panel2_1.add(germe2);
    		Panel2_1.add(modulo);
    		Panel2.add(Panel2_1, "2_1");
    		// JPanel pour le choix "GCL" : Panel2_2
    		Panel2_2.add(germe1);
    		Panel2_2.add(multiplicateur);
    		Panel2_2.add(incrément);
    		Panel2_2.add(modulo);
    		Panel2.add(Panel2_2, "2_2");
    		// JPanel pour le choix "LFSR" : Panel2_3
    		Panel2_3.add(germe1);
    		Panel2.add(Panel2_3, "2_3");
     
    		// JButton : test
    		test.addActionListener(this);
    		// test.setPreferredSize(new Dimension(150, 250));
    		// test.setLocation(400, 0);
    		container.add(test, BorderLayout.NORTH);
     
    		// JScrollPane (Scrollbar)
    		JScrollPane scroll = new JScrollPane(results,
    				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    		// results.add(scroll, null);
    		scroll.setBounds(new Rectangle(-4, 1, 397, 198));
     
    		// JTextArea des résultats
    		results.setPreferredSize(new Dimension(150, 400));
    		results.setLocation(600, 0);
    		results.setEditable(false);
    		container.add(results, BorderLayout.EAST);
    		// Modification de la sortie système (console)
    		JTextAreaOutputStream outStream = new JTextAreaOutputStream(results);
    		JTextAreaOutputStream errStream = new JTextAreaOutputStream(results);
    		System.setOut(new PrintStream(outStream));
    		System.setErr(new PrintStream(errStream));
     
    	}
     
    	public void actionPerformed(ActionEvent arg0) {
    		// Listener Button
    		for (int i = 0; i < 100; i++) {
    			System.out.println(i);
    		}
    	}
     
    	class ChoiceListener implements ActionListener {
    		// Listener des RadioButton (choix de la méthode)
    		public void actionPerformed(ActionEvent e) {
    			if (bg1.getSelection().getMnemonic() == 0) {
    				pile.show(Panel2, "2_0");
    			}
    			if (bg1.getSelection().getMnemonic() == 1) {
    				pile.show(Panel2, "2_1");
    			}
    			if (bg1.getSelection().getMnemonic() == 2) {
    				pile.show(Panel2, "2_2");
    			}
    			if (bg1.getSelection().getMnemonic() == 3) {
    				pile.show(Panel2, "2_3");
     
    			}
    		}
    	}
     
    	class JTextAreaOutputStream extends OutputStream {
    		// // Modification de la sortie système (console)
    		private JTextArea m_textArea = null;
     
    		/**
                     * Method JTextAreaOutputStream.
                     * 
                     * @param aTextArea
                     *            le JTextArea qui recevra les caractères.
                     */
    		public JTextAreaOutputStream(JTextArea aTextArea) {
    			m_textArea = aTextArea;
    		}
     
    		/**
                     * Écrit un caractère dans le JTextArea. Si le caractère est un retour
                     * chariot, scrolling.
                     * 
                     * @see java.io.OutputStream#write(int)
                     */
    		public void write(int b) throws IOException {
    			byte[] bytes = new byte[1];
    			bytes[0] = (byte) b;
    			String newText = new String(bytes);
    			m_textArea.append(newText);
    			if (newText.indexOf('\n') > -1) {
    				try {
    					m_textArea.scrollRectToVisible(m_textArea
    							.modelToView(m_textArea.getDocument().getLength()));
    				} catch (javax.swing.text.BadLocationException err) {
    					err.printStackTrace();
    				}
    			}
    		}
     
    		/**
                     * Écrit un tableau de bytes dans le JTextArea. Scrolling du JTextArea à
                     * la fin du texte ajouté.
                     * 
                     * @see java.io.OutputStream#write(byte[])
                     */
    		public final void write(byte[] arg0) throws IOException {
    			String txt = new String(arg0);
    			m_textArea.append(txt);
    			try {
    				m_textArea.scrollRectToVisible(m_textArea
    						.modelToView(m_textArea.getDocument().getLength()));
    			} catch (javax.swing.text.BadLocationException err) {
    				err.printStackTrace();
    			}
    		}
    	}
    }

    Encore MERCI

  2. #2
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    Citation Envoyé par NoNo282 Voir le message


    --> Le problème est que rien ne se passe quand je clique sur un RadioButton, si ce n'est plein d'exceptions
    C'est dommage de nous envahir de plein de code qui ne nous est pas (encore) utile et d'oublier l'indispensable: Nous donnes ces erreurs u'on aie une vague idée de ce qui se passe!

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2012
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2012
    Messages : 4
    Points : 5
    Points
    5
    Par défaut
    J'ai trouvé !

    Ou plutôt, on m'a trouvé la faute : le CardLayout n'était pas initialisé.

    Merci quand même.

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

Discussions similaires

  1. Comment utiliser CardLayout
    Par pwd75 dans le forum Agents de placement/Fenêtres
    Réponses: 2
    Dernier message: 10/02/2010, 12h16
  2. Réponses: 4
    Dernier message: 24/02/2009, 13h06
  3. comment utiliser actionscript ?
    Par webs dans le forum Flash
    Réponses: 3
    Dernier message: 10/02/2003, 00h11
  4. Comment utiliser OUT ?
    Par Bouziane Abderraouf dans le forum CORBA
    Réponses: 3
    Dernier message: 20/07/2002, 10h35
  5. Réponses: 5
    Dernier message: 11/06/2002, 16h21

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