package Chat;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.GridBagLayout;
import java.awt.event.*;
import java.io.*;
import java.util.*;
class FenetreClient extends JFrame implements ActionListener, FocusListener
{
private JMenuBar menuBar;
private JMenu file;
private JMenuItem exit, connectServer, joinChanel;
private LunchServer lunch;
private String contenuMessage;
private JTextField entree, dialogue;
private JDesktopPane desktop;
private ArrayList arrayList;
public FenetreClient()
{
super("client chat" );
setSize(800, 600);
//setResizable(false);
/* création des differents menus */
/********************************/
menuBar = new JMenuBar();
setJMenuBar(menuBar);
file = new JMenu("File" );
menuBar.add(file);
connectServer = new JMenuItem("connect server" );
file.add(connectServer);
connectServer.setEnabled(true);
connectServer.addActionListener(this);
joinChanel = new JMenuItem("join chanel" );
file.add(joinChanel);
joinChanel.setEnabled(false);
joinChanel.addActionListener(this);
exit = new JMenuItem("exit" );
file.add(exit);
exit.addActionListener(this);
arrayList = new ArrayList();
/* création de la frame principale pour supporter les
internes */ /***************************************************************/
desktop = new JDesktopPane();
Container contenu = getContentPane();
contenu.add(desktop);
}
public void actionPerformed(ActionEvent actionEvent)
{
if (actionEvent.getSource() == exit)
System.exit(0);
if (actionEvent.getSource() == connectServer)
{
connectServer.setEnabled(false);
joinChanel.setEnabled(true);
}
if (actionEvent.getSource() == joinChanel)
{
String txtChanel = JOptionPane.showInputDialog(null, "Nom du Chanel à rejoindre ou créer" );
if (txtChanel != null)
{
CreationChanel creationChanel = new CreationChanel(txtChanel, desktop);
arrayList.add(creationChanel);
}
}
}
public void focusGained(FocusEvent focusEvent)
{ }
public void focusLost(FocusEvent focusEvent)
{
contenuMessage = new String(entree.getText());
dialogue.setText(contenuMessage);
}
}
/* gestion des fenetres non modales */
class CreationChanel extends JFrame implements ActionListener, FocusListener
{
private JTextField entree;
private JTextArea dialogue;
private String newline = "\n";
public CreationChanel(String titre, JDesktopPane desktop)
{
/* création de frame internes pour les chanels de dialog */
/*********************************************************/
JInternalFrame jInternalFrame = new JInternalFrame(titre, true, true); // resizable, closable jInternalFrame.setBounds(10, 10, 300, 200); dialogue = new JTextArea();
dialogue.setEditable(false);
dialogue.setLineWrap(true);
dialogue.setWrapStyleWord(true);
JScrollPane jScrollPane = new JScrollPane(dialogue);
// force l'affichage de la barre de defilement
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
entree = new JTextField();
entree.setColumns(100);
// layout concernant l'internal frame
Container container = jInternalFrame.getContentPane();
container.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.weighty = 0.9; /* ampleur du composant par rapport aux autres composants (ici 90% de la place) */
constraints.weightx = 1.0; // ampleur du composant constraints.gridx = 0; // position verticale constraints.gridy = 0; // position horizontale constraints.fill = GridBagConstraints.BOTH; //container.add(dialogue, constraints); container.add(jScrollPane, constraints);
constraints.weighty = 0.1; // ampleur du composant constraints.gridy = 1;
constraints.fill = GridBagConstraints.BOTH;
container.add(entree, constraints);
entree.addFocusListener(this);
entree.addActionListener(this);
desktop.add(jInternalFrame, 0);
jInternalFrame.setVisible(true);
try
{
jInternalFrame.setSelected(true);
} catch(java.beans.PropertyVetoException ex){}
}
public void actionPerformed(ActionEvent actionEvent)
{ if (actionEvent.getSource() == entree)
{ if ((entree.getText()).length() != 0)
{
dialogue.append(entree.getText()+newline);
//dialogue.add(newline);
entree.setText("" );
}
}
}
public void focusLost(FocusEvent focusEvent)
{ if (focusEvent.getSource() == entree)
{
String contenu = entree.getText();
dialogue.setText(contenu);
}
}
public void focusGained(FocusEvent focusEvent)
{
}
}
public class IGClient
{
public static void main (String [] args)
{
FenetreClient fenetreClient = new FenetreClient();
fenetreClient.setVisible(true);
fenetreClient.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
voila le code c'est un peu plus simple à comprendre.
soyez indulgent cela fait peu que je pratique le java