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
|
package org.swng.test;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class Session extends JFrame {
private static final long serialVersionUID = 1L;
final static String PANE_CONNEXION = "PaneConnexion";
final static String PANE_APPLICATION = "PaneApplication";
private JPanel paneMain;
private CardLayout cardLayout;
private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
public Session() {
initComponents();
}
private void initComponents() {
JPanel paneConnexion = new PaneConnexion();
JPanel paneApplication = new PaneApplication();
// le conteneur principal qui contiendra les différents panels
paneMain = new JPanel();
paneMain.setLayout(new CardLayout());
cardLayout = (CardLayout)paneMain.getLayout();
paneMain.add(paneConnexion, PANE_CONNEXION);
paneMain.add(paneApplication, PANE_APPLICATION);
cardLayout.show(paneMain, PANE_CONNEXION);
this.getContentPane().add(paneMain);
}
private void connexionValide() {
cardLayout.show(paneMain, PANE_APPLICATION);
}
private static void installLnF() {
try {
String lnfClassname = PREFERRED_LOOK_AND_FEEL;
if (lnfClassname == null)
lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
UIManager.setLookAndFeel(lnfClassname);
} catch (Exception e) {
System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
+ " on this platform:" + e.getMessage());
}
}
public static void main(String[] args) {
installLnF();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Session frame = new Session();
frame.setDefaultCloseOperation(Session.EXIT_ON_CLOSE);
frame.setTitle("Session");
frame.setPreferredSize(new Dimension(400, 400));
frame.getContentPane().setPreferredSize(frame.getSize());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
private class PaneConnexion extends JPanel {
private static final long serialVersionUID = 3094601166018245873L;
private JTextField txtNom;
private JTextField txtCP;
private JPasswordField txtPassword;
private JLabel lblNom = new JLabel("Nom");
private JLabel lblCP = new JLabel("Code postal");
private JLabel lblPassword = new JLabel("Mot de passe");
private JLabel lblErreur = new JLabel("");
private JButton btnConnexion;
private PaneConnexion() {
super();
txtNom = new JTextField();
txtNom.setPreferredSize(new Dimension(200, 20));
txtCP = new JTextField();
txtCP.setPreferredSize(new Dimension(200, 20));
txtPassword = new JPasswordField();
txtPassword.setPreferredSize(new Dimension(200, 20));
lblErreur.setVisible(false);
lblErreur.setForeground(Color.RED);
btnConnexion = new JButton("Connexion");
btnConnexion.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
boolean result = controlerChamps();
if (!result)
lblErreur.setVisible(true);
else {
lblErreur.setVisible(false);
connexionValide();
}
}
});
PaneConnexion.this.setLayout(new GridBagLayout());
PaneConnexion.this.add(lblNom, new GridBagConstraints(0, 0, 1, 1, 0, 0,
GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(10, 0, 0, 10), 0, 0));
PaneConnexion.this.add(txtNom, new GridBagConstraints(1, 0, 1, 1, 0, 0,
GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(10, 0, 0, 0), 0, 0));
PaneConnexion.this.add(lblCP, new GridBagConstraints(0, 1, 1, 1, 0, 0,
GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(10, 0, 0, 10), 0, 0));
PaneConnexion.this.add(txtCP, new GridBagConstraints(1, 1, 1, 1, 0, 0,
GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(10, 0, 0, 0), 0, 0));
PaneConnexion.this.add(lblPassword, new GridBagConstraints(0, 2, 1, 1, 0, 0,
GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(10, 0, 0, 10), 0, 0));
PaneConnexion.this.add(txtPassword, new GridBagConstraints(1, 2, 1, 1, 0, 0,
GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(10, 0, 0, 0), 0, 0));
PaneConnexion.this.add(lblErreur, new GridBagConstraints(0, 3, 2, 1, 0, 0,
GridBagConstraints.PAGE_END, GridBagConstraints.NONE, new Insets(10, 0, 0, 0), 0, 0));
PaneConnexion.this.add(btnConnexion, new GridBagConstraints(0, 4, 2, 1, 0, 0,
GridBagConstraints.PAGE_END, GridBagConstraints.NONE, new Insets(10, 0, 0, 0), 0, 0));
}
private boolean controlerChamps() {
if (txtNom.getText().length() <= 0) {
txtNom.requestFocus();
lblNom.setForeground(Color.RED);
lblErreur.setText("Le champs Nom doit être renseigné");
return false;
}
else {
lblNom.setForeground(Color.BLACK);
if (txtCP.getText().length() <= 0) {
txtCP.requestFocus();
lblCP.setForeground(Color.RED);
lblErreur.setText("Le champs Code Postal doit être renseigné");
return false;
}
else {
lblCP.setForeground(Color.BLACK);
if (txtPassword.getPassword().length <= 0) {
txtPassword.requestFocus();
lblPassword.setForeground(Color.RED);
lblErreur.setText("Le champs Mot de passe doit être renseigné");
return false;
}
else
lblPassword.setForeground(Color.BLACK);
}
}
lblErreur.setText("");
return true;
}
}
private class PaneApplication extends JPanel {
private static final long serialVersionUID = 5993978962170965179L;
private PaneApplication() {
this.add(new JLabel("Ma super application"));
}
}
} |
Partager