CardLayout

Résolu
qhhu Messages postés 66 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 octobre 2009 - 5 févr. 2006 à 22:33
qhhu Messages postés 66 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 octobre 2009 - 6 févr. 2006 à 17:04
Salut
Voila j'ai fait un petit programme qui permet de transcrypter des fichiers au format voulu.
Pour ca j'utilise un CardLayout.
Seulement voila quant je compile le fichier, pas de problème, quand je l'exécute non plus mais quand j'appuie sur le boutton Suivant,
j'obtient un java.lang.IllegalArgumentException : wrong parent for CardLayout.
J'ai essayé plein de choses mais ca marche toujours pas.
Le programme n'est pas treminé vu qu'il se contente de stoker les bytes dans un tableau mais vous pouriez jeter un
petit coup d'oeil sur le code SVP Merci

import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class convert extends JFrame implements ActionListener{
ImageIcon suivant_img = new ImageIcon("suivant.jpg");
ImageIcon precedent_img = new ImageIcon("precedent.jpg");
JButton[] suivant = new JButton[4];
JButton[] precedent = new JButton[4];

CardLayout cards = new CardLayout();
int currentCard=0;

JLabel in_lab = new JLabel("1. Choisissez le fichier à convertir :");
JTextField in_field = new JTextField(20);
JButton parcourir = new JButton("Parcourir...");

JLabel format_lab = new JLabel("2. Sélectionnez le format de sortie :");
JComboBox format_box = new JComboBox();
String formats[] = {"mp3","wave","wma","midi"};
String formats2[] = {"mp3","wav","wma","mid"};

JLabel out_lab = new JLabel("3. Choisissez l'endroit ou enregistrer le fichier converti :");
JTextField out_field = new JTextField(20);
JButton parcourir2 = new JButton("Parcourir...");

JButton convertir = new JButton("Convertir");
JTextArea information = new JTextArea(10,30);
JScrollPane bbu=new JScrollPane(information, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
public convert(){
super("Convertisseur");
setDefaultCloseOperation(EXIT_ON_CLOSE);

for (int i=0; i < formats.length; i++){
format_box.addItem(formats[i]);
}
JPanel[] pane= new JPanel[4];
parcourir.addActionListener(this);
convertir.addActionListener(this);
pane[0] = new JPanel();
pane[0].add(in_lab);
pane[0].add(in_field);
pane[0].add(parcourir);

pane[1] = new JPanel();
pane[1].add(format_lab);
pane[1].add(format_box);

pane[2] = new JPanel();
pane[2].add(out_lab);
pane[2].add(out_field);
parcourir2.addActionListener(this);
pane[2].add(parcourir2);

pane[3] = new JPanel();
pane[3].add(convertir);
information.setEditable(false);
Color g = new Color(215,215,215);
information.setBackground(g);
information.setFont(new Font("Lucida Console",Font.PLAIN,11));
pane[3].add(bbu);

for(int d=0; d<=3; d++){
setLayout(new BorderLayout());
suivant[d] = new JButton("Suivant",suivant_img);
precedent[d] = new JButton("Precedent",precedent_img);
suivant[d].addActionListener(this);
precedent[d].addActionListener(this);
}
precedent[0].setEnabled(false);
suivant[3].setEnabled(false);
for(int t=0; t<=3; t++){
pane[t].add(precedent[t],BorderLayout.SOUTH);
pane[t].add(suivant[t],BorderLayout.SOUTH);
}
setLayout(cards);
add(pane[0],"card1");
add(pane[1],"card2");
add(pane[2],"card3");
add(pane[3],"card4");
setSize(550,300);
setVisible(true);
}

public void run(String in_path,String ext){
try{
FileInputStream file = new FileInputStream(in_path);
Vector v = new Vector();
int[] bytes;
boolean eof = false;
while(eof == false){
int in = file.read();
if(in != -1){
v.add(in);
}
else {
eof = true;
}
}
file.close();
bytes = new int[v.size()];
for(int j =0; j<=(v.size())-1; j++){
bytes[j] = v.get(j);
}
}
catch(IOException i){}
}
public void actionPerformed(ActionEvent evt){
Object source = evt.getSource();
if(source==parcourir){
JFileChooser dialogue = new JFileChooser();
String ecrivain;
File fichier;
if (dialogue.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
fichier = dialogue.getSelectedFile();
ecrivain = fichier.getPath();
in_field.setText(ecrivain);
}
}
if(source==parcourir2){
JFileChooser dialogue = new JFileChooser();
dialogue.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
String ecrivain;
File fichier;
if (dialogue.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
fichier = dialogue.getSelectedFile();
ecrivain = fichier.getPath();
out_field.setText(ecrivain);
}
}
if(source==suivant[0]||source==suivant[1]||source==suivant[2]){
currentCard++;
cards.show(this,"card"+currentCard);
}
if(source==precedent[1]||source==precedent[2]||source==precedent[3]){
currentCard--;
cards.show(this,"card"+currentCard);
}
if(source==convertir){
run(in_field.getText() , formats2[format_box.getSelectedIndex()]);
}

}
public static void main(String[] arguments){
convert n = new convert();
}
}

3 réponses

Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
5 févr. 2006 à 23:22
Sinon le parent ne peut pas etre une JFrame mais getContnentPane, enfin
vu qu getContentPane ne te retourne pas un JPanel je te conseil plus de
faire un JPanel intermediaire qui lui va avoir le layout avec un
CardLayout

WORA
3
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
5 févr. 2006 à 23:17
Salut,



qd tu creer tes panel utilise la methode setName comme ca tu auras moin de problème pour les trouver exemple



setLayout(cards);



for(int i = 0; i < pane.length; i++){

pane[i] = new JPanel();

pane[i].setName("card" + (i+1));

add(pane[i],"card" + (i+1));

}



WORA
0
qhhu Messages postés 66 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 octobre 2009
6 févr. 2006 à 17:04
Merci beaucoup C'est exactement ca qu'il me fallait!!!
0
Rejoignez-nous