probleme de fermeture JFrame - Java - Programmation
MarshPosté le 29-12-2012 à 18:27:14
Salut le problème c'est que l'application ne se ferme plus et il s'affiche en noir voilà le code source import java.awt.Container; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.HashSet; import java.util.Iterator; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
class Serveur extends JFrame implements ActionListener { HashSet<String> names = new HashSet<String>(); private static HashSet<PrintWriter> writers = new HashSet<PrintWriter>();
private JPanel pan1, pan2,pan3; private JLabel port,nbMax ; private JTextField fport,fnbMax; private JButton ecouter ; ServerSocket serv ; Serveur() { setSize(460, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setLayout(new GridLayout(7, 1)); pan1 = new JPanel(); port = new JLabel("Port: " ); port.setPreferredSize(new Dimension(70, 30)); fport = new JTextField(15); nbMax = new JLabel("Nombre maximum de clients : " ); nbMax.setPreferredSize(new Dimension(90, 30)); fnbMax = new JTextField(15); pan1.add(port); pan1.add(fport); pan2 = new JPanel(); pan2.add(nbMax); pan2.add(fnbMax); pan3 = new JPanel(); ecouter = new JButton("Ecouter" ); pan3.add(ecouter); Container c = getContentPane(); c.add(pan1); c.add(pan2); c.add(pan3); ecouter.addActionListener(this); }
@Override public void actionPerformed(ActionEvent ae) { String cmd=(String)ae.getActionCommand(); if(cmd.equals("Ecouter" )) {int I1=Integer.parseInt(fport.getText()); int I2= Integer.parseInt(fnbMax.getText());
try{ serv = new ServerSocket(I1);
while(true){ ClientHandler w;
//server.accept returns a client connection Socket soc=serv.accept(); PrintWriter writer = new PrintWriter(soc.getOutputStream()); writers.add(writer); w = new ClientHandler(soc,writer); Thread t = new Thread(w); t.start(); }} catch (IOException e) { } finally { try { serv.close(); } catch (IOException ex) { } } }
}
public class ClientHandler implements Runnable { BufferedReader reader; Socket sock; PrintWriter client;
public ClientHandler(Socket clientSocket, PrintWriter user) { // new inputStreamReader and then add it to a BufferedReader client = user; try { sock = clientSocket; InputStreamReader isReader = new InputStreamReader(sock.getInputStream()); reader = new BufferedReader(isReader); } // end try catch (Exception ex) { System.out.println("error beginning StreamReader" ); } // end catch
public void tellEveryone(String message) { // sends message to everyone connected to server Iterator it = writers.iterator();
while (it.hasNext()) { try { PrintWriter writer = (PrintWriter) it.next(); writer.println(message); System.out.println("Sending" + message); writer.flush(); } // end try catch (Exception ex) { System.out.println("error telling everyone" ); } // end catch } // end while } // end tellEveryone() } public class Main {
Marsh Posté le 29-12-2012 à 18:27:14
Salut
le problème c'est que l'application ne se ferme plus et il s'affiche en noir
voilà le code source
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
class Serveur extends JFrame implements ActionListener {
HashSet<String> names = new HashSet<String>();
private static HashSet<PrintWriter> writers = new HashSet<PrintWriter>();
private JPanel pan1, pan2,pan3;
private JLabel port,nbMax ;
private JTextField fport,fnbMax;
private JButton ecouter ;
ServerSocket serv ;
Serveur() {
setSize(460, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(7, 1));
pan1 = new JPanel();
port = new JLabel("Port: " );
port.setPreferredSize(new Dimension(70, 30));
fport = new JTextField(15);
nbMax = new JLabel("Nombre maximum de clients : " );
nbMax.setPreferredSize(new Dimension(90, 30));
fnbMax = new JTextField(15);
pan1.add(port);
pan1.add(fport);
pan2 = new JPanel();
pan2.add(nbMax);
pan2.add(fnbMax);
pan3 = new JPanel();
ecouter = new JButton("Ecouter" );
pan3.add(ecouter);
Container c = getContentPane();
c.add(pan1); c.add(pan2); c.add(pan3);
ecouter.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent ae) {
String cmd=(String)ae.getActionCommand();
if(cmd.equals("Ecouter" ))
{int I1=Integer.parseInt(fport.getText());
int I2= Integer.parseInt(fnbMax.getText());
try{
serv = new ServerSocket(I1);
while(true){
ClientHandler w;
//server.accept returns a client connection
Socket soc=serv.accept();
PrintWriter writer = new PrintWriter(soc.getOutputStream());
writers.add(writer);
w = new ClientHandler(soc,writer);
Thread t = new Thread(w);
t.start();
}} catch (IOException e) {
}
finally {
try {
serv.close();
} catch (IOException ex) {
}
}
}
}
public class ClientHandler implements Runnable {
BufferedReader reader;
Socket sock;
PrintWriter client;
public ClientHandler(Socket clientSocket, PrintWriter user) {
// new inputStreamReader and then add it to a BufferedReader
client = user;
try {
sock = clientSocket;
InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(isReader);
} // end try
catch (Exception ex) {
System.out.println("error beginning StreamReader" );
} // end catch
} // end ClientHandler()
public void run() {
String message;
String[] data;
String connect = "Connect";
String disconnect = "Disconnect";
String chat = "Chat";
try {
while ((message = reader.readLine()) != null) {
System.out.println("Received: " + message);
data = message.split("¥" );
for (String token:data) {
System.out.println(token);
}
if (data[2].equals(connect)) {
tellEveryone((data[0] + "¥" + data[1] + "¥" + chat));
userAdd(data[0]);
} else if (data[2].equals(disconnect)) {
tellEveryone((data[0] + "¥has disconnected." + "¥" + chat));
userRemove(data[0]);
} else if (data[2].equals(chat)) {
tellEveryone(message);
} else {
System.out.println("No Conditions were met." );
}
} // end while
} // end try
catch (Exception ex) {
System.out.println("lost a connection" );
writers.remove(client);
} // end catch
} // end run()
} // end class ClientHandler
public void userAdd (String data) {
String message;
String add = "¥ ¥Connect", done = "Server¥ ¥Done";
names.add(data);
String[] tempList = new String[(names.size())];
names.toArray(tempList);
for (String token:tempList) {
message = (token + add);
tellEveryone(message);
}
tellEveryone(done);
}
public void userRemove (String data) {
String message;
String add = "¥ ¥Connect", done = "Server¥ ¥Done";
names.remove(data);
String[] tempList = new String[(names.size())];
names.toArray(tempList);
for (String token:tempList) {
message = (token + add);
tellEveryone(message);
}
tellEveryone(done);
}
public void tellEveryone(String message) {
// sends message to everyone connected to server
Iterator it = writers.iterator();
while (it.hasNext()) {
try {
PrintWriter writer = (PrintWriter) it.next();
writer.println(message);
System.out.println("Sending" + message);
writer.flush();
} // end try
catch (Exception ex) {
System.out.println("error telling everyone" );
} // end catch
} // end while
} // end tellEveryone()
}
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Serveur serv =new Serveur();
serv. setVisible(true);
}
}
s'il y a qq1 qui peut m'éclairer ?
Merci d'avance!