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
| public class LoginDemo extends JPanel {
private static final long serialVersionUID = 1L;
private JTextField userNameField;
private JPasswordField passwordField;
private JButton buttonLogin;
private JButton buttonCancel;
public LoginDemo() {
setLayout(new GridBagLayout());
Insets insets = new Insets(2,2,2,2);
add(new JLabel("Nom : "), new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.BASELINE_TRAILING, GridBagConstraints.NONE, insets, 0, 0));
userNameField = new JTextField( AppPreferences.getLastUserName(), 20 );
add(userNameField, new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.BASELINE_LEADING, GridBagConstraints.BOTH, insets, 0, 0));
add(new JLabel("Mot de passe: "), new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.BASELINE_TRAILING, GridBagConstraints.NONE, insets, 0, 0));
passwordField = new JPasswordField(20);
add(passwordField , new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.BASELINE_LEADING, GridBagConstraints.BOTH, insets, 0, 0));
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
add(buttonPanel , new GridBagConstraints(0, 2, 2, 2, 1, 0, GridBagConstraints.BASELINE, GridBagConstraints.BOTH, insets, 0, 0));
buttonLogin = new JButton("Connexion");
buttonLogin.setEnabled(false);
buttonPanel.add(buttonLogin);
buttonLogin.addActionListener(e-> connection());
buttonCancel = new JButton("Annuler");
buttonPanel.add(buttonCancel);
buttonCancel.addActionListener(e-> SwingUtilities.getWindowAncestor(this).dispatchEvent(new WindowEvent(
SwingUtilities.getWindowAncestor(this), WindowEvent.WINDOW_CLOSING)));
DocumentListener documentListener = new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
handleLoginButton();
}
@Override
public void insertUpdate(DocumentEvent e) {
handleLoginButton();
}
@Override
public void changedUpdate(DocumentEvent e) {
handleLoginButton();
}
private void handleLoginButton() {
buttonLogin.setEnabled(!userNameField.getText().isEmpty()&&passwordField.getPassword().length!=0);
}
};
userNameField.getDocument().addDocumentListener(documentListener);
passwordField.getDocument().addDocumentListener(documentListener);
}
private void connection() {
String userName = userNameField.getText();
if ( !userName.isEmpty() ) {
char[] password = passwordField.getPassword();
if ( verifyPassword(userName, password) ) {
AppPreferences.saveUserName(userName);
doConnection(userName);
}
}
}
private boolean verifyPassword(String userName, char[] password) {
return ...; // tu fais la vérification du mot de passe
}
private void doConnection(String userName) {
// ce que tu dois faire...
}
public static void main(String[] args) {
JFrame frame = new JFrame("Démo connexion");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new LoginDemo());
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
} |
Partager