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
| package client;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class InterfaceGraphique extends JFrame {
private static final long serialVersionUID = -2040488316874518030L;
private JPanel panel = new JPanel ();
private JPanel result = new JPanel ();
private JPanel panel2 = new JPanel ();
private JTextArea envoieTexte = new JTextArea (5, 55);
private JTextArea receptionTexte = new JTextArea (20, 55);
private JScrollPane scrollRecept = new JScrollPane ();
private JScrollPane scrollEnvoi = new JScrollPane ();
private JTextField serveur = new JTextField ();
private JTextField login = new JTextField ();
private JLabel label1 = new JLabel ("Entrez le nom ou l'adresse IP du serveur");
private JLabel label2 = new JLabel ("Entrez votre login");
JDialog dialog = new JDialog (this, "Connexion", true);
private String name;
private Client client;
public InterfaceGraphique () {
this.buildDialog ();
client.connection (); //ici
client.envoie ();
this.build (); //On initialise notre fenetre
}
private void buildDialog () {
JButton bouton = new JButton (new OkAction (this, "OK"));
InputMap inputMap = this.serveur.getInputMap ();
ActionMap actionMap = this.serveur.getActionMap ();
InputMap inputMap2 = this.login.getInputMap ();
ActionMap actionMap2 = this.login.getActionMap ();
this.panel2.setLayout (new BoxLayout (panel2, BoxLayout.Y_AXIS));
this.serveur.setAlignmentX (Component.CENTER_ALIGNMENT);
this.login.setAlignmentX (Component.CENTER_ALIGNMENT);
this.label1.setAlignmentX (Component.CENTER_ALIGNMENT);
this.label2.setAlignmentX (Component.CENTER_ALIGNMENT);
bouton.setAlignmentX (Component.CENTER_ALIGNMENT);
this.panel2.add (this.label1);
this.panel2.add (this.serveur);
this.panel2.add (this.label2);
this.panel2.add (this.login);
this.panel2.add (bouton);
this.dialog.setLocationRelativeTo (null);
this.dialog.setResizable (false);
this.dialog.add (this.panel2);
this.dialog.pack ();
inputMap.put (KeyStroke.getKeyStroke (KeyEvent.VK_ENTER, 0), "ok");
actionMap.put ("ok", new OkAction (this, "OK"));
inputMap2.put (KeyStroke.getKeyStroke (KeyEvent.VK_ENTER, 0), "ok");
actionMap2.put ("ok", new OkAction (this, "OK"));
this.dialog.setVisible (true); //ici
}
private void build () {
InputMap inputMap = this.envoieTexte.getInputMap ();
ActionMap actionMap = this.envoieTexte.getActionMap ();
JMenuBar menuBar = new JMenuBar ();
JMenu menu1 = new JMenu ("Fichier");
JMenuItem quitter = new JMenuItem (new QuitterAction (this, "Quitter"));
JMenu menu2 = new JMenu ("?");
JMenuItem aPropos = new JMenuItem (new AProposAction (this, "A propos"));
menu1.add (quitter);
menuBar.add (menu1);
menu2.add (aPropos);
menuBar.add (menu2);
this.setJMenuBar (menuBar);
this.setTitle ("Tchat"); //On donne un titre a l'application
this.setLocationRelativeTo (null); //On centre la fenêtre sur l'écran
this.setResizable (true); //On autorise le redimensionnement de la fenetre
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); //On dit a l'application de se fermer lors du clic sur la croix
this.setMinimumSize (new Dimension (250, 250));
this.getContentPane ().add (this.buildPanel (), BorderLayout.CENTER);
this.getContentPane ().add (this.buildResult (), BorderLayout.SOUTH);
this.envoieTexte.requestFocusInWindow ();
this.pack ();
inputMap.put (KeyStroke.getKeyStroke (KeyEvent.VK_ENTER, 0), "envoiTexte");
actionMap.put ("envoiTexte", new EnvoiAction (this, this.name));
inputMap.put (KeyStroke.getKeyStroke (KeyEvent.VK_ESCAPE, 0), "quitter");
actionMap.put ("quitter", new QuitterAction (this, ""));
}
private JPanel buildPanel () {
this.panel.setLayout (new BorderLayout (5,5));
this.receptionTexte.setLineWrap (true);
this.receptionTexte.setEditable (false);
this.scrollRecept.setViewportView (this.receptionTexte);
this.panel.add (this.scrollRecept);
return this.panel;
}
private JPanel buildResult () {
this.result.setLayout (new BorderLayout (5, 5));
this.envoieTexte.setLineWrap (true);
this.scrollEnvoi.setViewportView (this.envoieTexte);
this.result.add (this.scrollEnvoi);
return this.result;
}
public JTextArea getEnvoieTexte () {
return this.envoieTexte;
}
public JTextArea getReceptionTexte () {
return this.receptionTexte;
}
public JTextField getServeur () {
return this.serveur;
}
public JTextField getLogin () {
return this.login;
}
public void setName (String name) {
this.name = name;
}
public JDialog getDialog () {
return this.dialog;
}
} |
Partager