Bonjour,

Le mot de passe récupéré par un champ texte qui se trouve dans une interface graphique est stocké caractère par caractère dans un fichier texte avec le Relative TimeStamp : 2011-07-06 15:24:16.876

Example Demandé: Mot de passe tapé par l'utilisateur : abc

Fichier Correspondant contient :

2011-07-06 15:24:16.876 a
2011-07-06 15:24:16.878 b
2011-07-06 15:24:16.879 c

Mon souci c'est que le fichier affiche que le dernier caractère saisi dans le champ password.

Example Réel: Mot de passe tapé par l'utilisateur : abc

Fichier Correspondant contient :

2011-07-06 15:24:16.879 c


Voici le code:

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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
 
import java.io.*;
import java.sql.Timestamp;
import java.util.Date;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
 
 
public class Login extends JFrame
{
	// Variables declaration
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JTextField jTextField1;
	private JPasswordField jPasswordField1;
	private JButton jButton1;
	private JPanel contentPane;
	// End of variables declaration
 
 
	public Login()
	{
		super();
		create();
	this.setVisible(true);
	}
 
 
	private void create()
	{
		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		jTextField1 = new JTextField();
		jPasswordField1 = new JPasswordField();
		jButton1 = new JButton();
		contentPane = (JPanel)this.getContentPane();
 
		//
		// jLabel1
		//
		jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
		jLabel1.setForeground(new Color(0, 0, 255));
		jLabel1.setText("Username:");
 
		//
		// jLabel2
		//
		jLabel2.setHorizontalAlignment(SwingConstants.LEFT);
		jLabel2.setForeground(new Color(0, 0, 255));
		jLabel2.setText("Password:");
 
		//
		// jTextField1
		//
		jTextField1.setForeground(new Color(0, 0, 255));
		jTextField1.setSelectedTextColor(new Color(0, 0, 255));
		jTextField1.setToolTipText("Enter your username");
		jTextField1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				jTextField1_actionPerformed(e);
			}
 
		});
 
		//jTextField1.addKeyListener(new RecepteurClavier());
 
		//
		// jPasswordField1
		//
		jPasswordField1.setForeground(new Color(0, 0, 255));
		jPasswordField1.setToolTipText("Enter your password");
		jPasswordField1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				jPasswordField1_actionPerformed(e);
			}
		});
 
		jPasswordField1.addKeyListener(new RecepteurClavier()); // auquel on rajoute un recepteur clavier
 
 
		//
		// jButton1
		//
		jButton1.setBackground(new Color(204, 204, 204));
		jButton1.setForeground(new Color(0, 0, 255));
		jButton1.setText("Login");
		jButton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				jButton1_actionPerformed(e);
			}
 
		});
 
		//
		// contentPane
		//
		contentPane.setLayout(null);
		contentPane.setBorder(BorderFactory.createEtchedBorder());
		contentPane.setBackground(new Color(204, 204, 204));
		addComponent(contentPane, jLabel1, 5,10,106,18);
		addComponent(contentPane, jLabel2, 5,47,97,18);
		addComponent(contentPane, jTextField1, 110,10,183,22);
		addComponent(contentPane, jPasswordField1, 110,45,183,22);
		addComponent(contentPane, jButton1, 150,75,83,28);
 
		//
		// login
		//
		this.setTitle("Login To Members Area");
		this.setLocation(new Point(76, 182));
		this.setSize(new Dimension(335, 141));
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setResizable(false);
	}
 
 
 
	/** Add Component Without a Layout Manager (Absolute Positioning) */
	private void addComponent(Container container,Component c,int x,int y,int width,int height)
	{
		c.setBounds(x,y,width,height);
		container.add(c);
	}
 
 
	private void jTextField1_actionPerformed(ActionEvent e)
	{
 
	}
 
 
	private void jPasswordField1_actionPerformed(ActionEvent e)
	{
 
	}
 
 
	private void jButton1_actionPerformed(ActionEvent e)
	{
		System.out.println("\njButton1_actionPerformed(ActionEvent e) called.");
		String username = new String(jTextField1.getText());
		String password = new String(jPasswordField1.getText());
 
		if(username.equals("") || password.equals("")) // If password and username is empty > Do this >>>
		{
			jButton1.setEnabled(false);
			JLabel errorFields = new JLabel("<HTML><FONT COLOR = Blue>You must enter a username and password to login.</FONT></HTML>");
			JOptionPane.showMessageDialog(null,errorFields);
			jTextField1.setText("");
			jPasswordField1.setText("");
			jButton1.setEnabled(true);
			this.setVisible(true);
		}
		else
		{
			JLabel optionLabel = new JLabel("<HTML><FONT COLOR = Blue>You entered</FONT><FONT COLOR = RED> <B>"+username+"</B></FONT> <FONT COLOR = Blue>as your username.<BR> Is this correct?</FONT></HTML>");
			int confirm =JOptionPane.showConfirmDialog(null,optionLabel);
			switch(confirm)
			{ // Switch > Case
				case JOptionPane.YES_OPTION: // Attempt to Login user
					jButton1.setEnabled(false); // Set button enable to false to prevent 2 login attempts
					break;
 
				case JOptionPane.NO_OPTION: // No Case.(Go back. Set text to 0)
					jButton1.setEnabled(false);
					jTextField1.setText("");
					jPasswordField1.setText("");
					jButton1.setEnabled(true);
					break;
 
				case JOptionPane.CANCEL_OPTION: // Cancel Case.(Go back. Set text to 0)
					jButton1.setEnabled(false);
					jTextField1.setText("");
					jPasswordField1.setText("");
					jButton1.setEnabled(true);
					break;
 
			} // End Switch > Case
 
 
		}
	}
 
 
 
 	public static void ecrireFichier(String path, char letter) 
	{
		PrintWriter ecri = null;
 
		try
		{
			ecri = new PrintWriter(new FileWriter(path));
			char espace = ' ';
 
			java.util.Date date= new java.util.Date();
  			ecri.print(new Timestamp(date.getTime()));
			ecri.print(espace);
    			ecri.println(letter);
 
		}
		catch (NullPointerException a)
		{
			System.out.println("Erreur : pointeur null");
		}
		catch (IOException a)
		{
			System.out.println("Problème d'IO");
		}
 
		if(ecri != null)
		{
		ecri.flush();
		ecri.close();
		}
 
 
	}
 
 
 
	public static void main(String[] args)
	{
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		try
		{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		}
		catch (Exception ex)
		{
		System.out.println("Failed loading L&F: ");
		System.out.println(ex);
		}
		new Login();
	};
 
 
 
	public class RecepteurClavier implements KeyListener
	{
 
 
		public void keyPressed(KeyEvent event)
		{
 
			char letter = event.getKeyChar();
		        Login.ecrireFichier( "Password.txt", letter);
 
 
		}
 
 
		public void keyReleased(KeyEvent event) {}
 
		public void keyTyped(KeyEvent event) {}    	
 
 
 
	}
 
}