Problème code Java

Résolu
toledo9 Messages postés 22 Date d'inscription mardi 25 janvier 2011 Statut Membre Dernière intervention 31 mars 2022 - 23 févr. 2012 à 06:32
toledo9 Messages postés 22 Date d'inscription mardi 25 janvier 2011 Statut Membre Dernière intervention 31 mars 2022 - 24 févr. 2012 à 06:18
Bonjour,

Problème code Java

J'aimerais que dans jTextArea les (nom, prénom, tél) soient séparés.

Comment mettre une action sur mon bouton enregistrer ? pour que celui ci enregistre le résultat dans un fichier "repertoire.txt"
Comment mettre une action sur mon bouton annuler ? pour que celui ci efface le résultat dans le jTextArea
Comment mettre une action sur mon bouton recherche ? pour que celui ci recherche dans le fichier "repertoire.txt".

Voici mon code :

public class NewJFrame extends javax.swing.JFrame {

/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton2 = new javax.swing.JButton();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("Prénom :");

jButton1.setText("OK");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

jButton2.setText("Quitter");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setText("Effacer");
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton3MouseClicked(evt);
}
});

jButton4.setText("Enregistrer");

jButton5.setText("Recherche");

jLabel2.setText("Nom :");

jLabel3.setText("Tél :");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(39, 39, 39)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 79, Short.MAX_VALUE)
.addComponent(jButton2))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
.addComponent(jTextField3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addComponent(jButton1)))
.addGap(34, 34, 34))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton4)
.addComponent(jButton3)
.addComponent(jButton5))
.addContainerGap(40, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String Chaine1, Chaine2, Chaine3;

Chaine1 = jTextField1.getText();
Chaine2 =jTextField2.getText();
Chaine3 =jTextField3.getText();
jTextArea1.setText("Bonjour " + Chaine1 + Chaine2 + Chaine3);
}

private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
// End of variables declaration
}


Merci,

5 réponses

cormandyr Messages postés 600 Date d'inscription samedi 20 mai 2006 Statut Membre Dernière intervention 8 juillet 2016 11
23 févr. 2012 à 08:33
Salut,

je ne sais pas quel outil tu as utilisé pour faire ton interface graphique et en plus, n'en n'ayant jamais utilisé je ne pourrai te dire comment le faire via cet outil, mais :

[list]
l'ajout d'action sur des boutons se fait via un ActionListener (tuto).

ecrire dans un fichier texte, je te conseille le BufferedWriter tuto

effacer la contenu du JTextArea, cela se fait via la methode setText(null)

et la recherche se fait apres une lecture du fichier (BufferedReader par exemple) et puis un simple code fait maison de recherche
/list

Voila je ne pense pas pouvoir dire autre chose ^^ si jamais, n'hésite pas à revenir pour plus d'info sur un point ou l'autre ^^
3
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
23 févr. 2012 à 08:36
Bonjour,

Sur un JButton, on n'utilise pas de MouseListener mais des ActionListener :

jbutton1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
//Ici, tu places le code qui sera exécuté lors du clic sur le bouton
}
});


Pour vider la jtextArea :
jTextArea.clear();
jTextArea.repaint();


Pour tes boutons de recherche dans le fichier texte, ce n'est pas aussi immédiat : tu peux charger les données du fichier dans une liste et faire la recherche.
0
toledo9 Messages postés 22 Date d'inscription mardi 25 janvier 2011 Statut Membre Dernière intervention 31 mars 2022
23 févr. 2012 à 12:04
Extrait code pour bouton enregistrer

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter("IODemo.txt"));
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
try {
out.write("asdf");
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
try {
out.close();
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}

Ca ne marche pas comme je veux. Pourquoi ?

J'ai mis l'action sur un bouton

Merci pour la réponse.
0
cormandyr Messages postés 600 Date d'inscription samedi 20 mai 2006 Statut Membre Dernière intervention 8 juillet 2016 11
23 févr. 2012 à 13:48
en combinant le tout ça donne... en théorie, cela devrait marcher....

jbutton1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
          BufferedWriter out = null; 
          try { 
             out = new BufferedWriter(new FileWriter("IODemo.txt")); 
             out.write("asdf"); 
             out.close(); 
         } catch (IOException ex) { 
             Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex); 
         }
      }
 
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
toledo9 Messages postés 22 Date d'inscription mardi 25 janvier 2011 Statut Membre Dernière intervention 31 mars 2022
24 févr. 2012 à 06:18
1.L'action sur le bouton enregistrer fonctionne mais celui ci ecrit seulement "adsf" alors que moi je veux qu'il enregistre dans un fichier .txt ce que je tape dans mes champs nom, prénom, tél
Ensuite il se remet à zéro pour la saisie d'un nouveau "nom, prénom, tél

2. pour le bouton effacer

public void actionPerformed(ActionEvent e) {
jTextArea.Clear();
JTextArea.repaint();

3. Comment utiliser ArrayList pour le bouton recherche ?

Merci,
0
Rejoignez-nous