Problème code java

Fermé
westerndigit Messages postés 134 Date d'inscription mercredi 16 février 2011 Statut Membre Dernière intervention 18 avril 2012 - Modifié par westerndigit le 23/02/2012 à 06:26
irmeche Messages postés 71 Date d'inscription jeudi 10 juin 2010 Statut Membre Dernière intervention 18 juillet 2014 - 25 févr. 2012 à 09:08
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 https://docs.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,

A voir également:

1 réponse

irmeche Messages postés 71 Date d'inscription jeudi 10 juin 2010 Statut Membre Dernière intervention 18 juillet 2014 17
Modifié par irmeche le 25/02/2012 à 09:41
voila ton programme

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

public class NewJFrame extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;
	
	private JButton jButton1;
	private JButton jButton2;
	private JButton jButton3;
	private JButton jButton4;
	private JButton jButton5;
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JScrollPane jScrollPane1;
	private JTextArea jTextArea1;
	private JTextField jTextField1;
	private JTextField jTextField2;
	private JTextField jTextField3;

	public NewJFrame() {
		initComponents();
	}

	private void initComponents() {
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
		jTextField1 = new JTextField();
		jLabel1 = new JLabel();
		jButton1 = new JButton();
		jScrollPane1 = new JScrollPane();
		jTextArea1 = new JTextArea();
		jButton2 = new JButton();
		jTextField2 = new JTextField();
		jTextField3 = new JTextField();
		jButton3 = new JButton();
		jButton4 = new JButton();
		jButton5 = new JButton();
		jLabel2 = new JLabel();
		jLabel3 = new JLabel();
		
		jLabel1.setText("Prénom :");
		jLabel2.setText("Nom :");
		jLabel3.setText("Tél :");
		
		jTextArea1.setColumns(20);
		jTextArea1.setRows(5);
		jScrollPane1.setViewportView(jTextArea1);
		
		jButton1.setText("OK");
		jButton1.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				String Chaine1, Chaine2, Chaine3;
				Chaine1 = jTextField1.getText();
				Chaine2 = jTextField2.getText();
				Chaine3 = jTextField3.getText();
				jTextArea1.setText("Bonjour " + Chaine1 + " " + Chaine2 + " " + Chaine3);
			}
		});
		

		jButton2.setText("Quitter");
		jButton2.addActionListener(	new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				System.exit(0);
			}
		});

		jButton3.setText("Effacer");
		jButton3.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				jTextArea1.setText("");
			}
		});

		jButton4.setText("Enregistrer");
		jButton4.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				String text = jTextArea1.getText();
				String adressedufichier = "C:/repertoire.txt";
				try {
					FileWriter fw = new FileWriter(adressedufichier, true);
					BufferedWriter output = new BufferedWriter(fw);
					output.write(text);
					output.flush();
					output.close();
				} catch (IOException ioe) {
					ioe.printStackTrace();
				}
			}
		});

		jButton5.setText("Recherche");
		jButton5.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				String adressedufichier = "C:/repertoire.txt";
				String text = "";
				try {
					String ligne;
					InputStream ips = new FileInputStream(adressedufichier);
					InputStreamReader ipsr = new InputStreamReader(ips);
					BufferedReader lecteurAvecBuffer = new BufferedReader(ipsr);
					while ((ligne = lecteurAvecBuffer.readLine()) != null) {
						text += ligne + "\n";
					}
					lecteurAvecBuffer.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				jTextArea1.setText(text);
			}
		});

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

		pack();
	}	

	public static void main(String args[]) {
		try {
			for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (Exception ex) {
			Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
		} 
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				new NewJFrame().setVisible(true);
			}
		});
	}
}
0