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
|
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
public class Fenetre extends JPanel implements ActionListener
{
private static final long serialVersionUID = 1L;
private static String OK = "ok";
private static String HELP = "help";
private JFrame controllingFrame; //Nécessaire pour les dialogues
private JPasswordField passwordField;
public FireWall(JFrame f) {
//J'utilise le FlowLayout par défaut.
controllingFrame = f;
//Création des différentes variables
passwordField = new JPasswordField(10);
passwordField.setActionCommand(OK);
passwordField.addActionListener(this);
JLabel label = new JLabel("Entrer le mot de passe");
label.setLabelFor(passwordField);
JComponent buttonPane = createButtonPanel();
// Disposition de tout les éléments
JPanel textPane = new JPanel(new FlowLayout(FlowLayout.TRAILING));
textPane.add(label);
textPane.add(passwordField);
add(textPane);
add(buttonPane);
}
protected JComponent createButtonPanel() {
JPanel p = new JPanel(new GridLayout(0,1));
JButton okButton = new JButton("OK");
JButton helpButton = new JButton("Help");
okButton.setActionCommand(OK);
helpButton.setActionCommand(HELP);
okButton.addActionListener(this);
helpButton.addActionListener(this);
p.add(okButton);
p.add(helpButton);
return p;
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (OK.equals(cmd)) { //Traitement du mot de passe.
char[] input = passwordField.getPassword();
if (isPasswordCorrect(input)) {
JOptionPane.showMessageDialog(controllingFrame, "Bon mot de passe");
} else {
JOptionPane.showMessageDialog(controllingFrame,
"Mot de passe incorrect. Réessayer.",
"Message d'erreur",
JOptionPane.ERROR_MESSAGE);
}
//Mettre à zéro le mot de passe, pour plus de sécurité.
Arrays.fill(input, '0');
passwordField.selectAll();
}
else { //Demande d'aide
JOptionPane.showMessageDialog(controllingFrame,
"Si vous navais pas le mot de passe, veuillez contacter\n"
+ "le propriétaire de ce logiciel \"test\"");
}
}
private static boolean isPasswordCorrect(char[] input) {
boolean isCorrect = true;
char[] correctPassword = { 'R', 'e', 't', 'h', 'e', 'r', '.', 'C', 'o', 'r', 'p'};
if (input.length != correctPassword.length) {
isCorrect = false;
} else {
isCorrect = Arrays.equals (input, correctPassword);
}
return isCorrect;
}
public FireWall ()
{
//Création et configuration de la fenêtre.
JFrame frame = new JFrame("FireWall");
frame.setPreferredSize(new Dimension(400, 115));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Fenetre newContentPane = new Fenetre(frame);
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
// Affiche la fenêtre.
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
} |
Partager