implémentation de l'interface graphique avec Java Swing
This commit is contained in:
parent
8362b82a88
commit
3a8a8ef7f1
4 changed files with 177 additions and 118 deletions
176
src/chat/ClientWindow.java
Normal file
176
src/chat/ClientWindow.java
Normal file
|
@ -0,0 +1,176 @@
|
|||
package chat;
|
||||
import java.net.*;
|
||||
import javax.swing.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
|
||||
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.net.DatagramSocket;
|
||||
|
||||
class SendThread extends Thread {
|
||||
String username;
|
||||
String address;
|
||||
String message;
|
||||
int port;
|
||||
|
||||
SendThread(String in_username, String in_address, String in_message, int in_port)
|
||||
{
|
||||
username = in_username;
|
||||
address = in_address;
|
||||
message = in_message;
|
||||
port = in_port;
|
||||
}
|
||||
public void run()
|
||||
{
|
||||
Socket link;
|
||||
Boolean connected = false;
|
||||
PrintWriter out;
|
||||
|
||||
while(!connected)
|
||||
{
|
||||
try
|
||||
{
|
||||
link = new Socket(address, port);
|
||||
out = new PrintWriter(link.getOutputStream(),true);
|
||||
out.println(username + " : " + message);
|
||||
|
||||
connected = true;
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
System.out.println("nik1!");
|
||||
connected = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class ReceiveThread extends Thread {
|
||||
|
||||
int port;
|
||||
JTextArea displayArea;
|
||||
ReceiveThread(int in_port, JTextArea in_displayArea)
|
||||
{
|
||||
port = in_port;
|
||||
displayArea = in_displayArea;
|
||||
}
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
ServerSocket servSocket = new ServerSocket(port);
|
||||
Boolean exit = false;
|
||||
|
||||
while(!exit)
|
||||
{
|
||||
Socket link = servSocket.accept();
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream()));
|
||||
String message = in.readLine();
|
||||
displayArea.append(message + "\n");
|
||||
displayArea.setCaretPosition(displayArea.getDocument().getLength());
|
||||
link.close();
|
||||
}
|
||||
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
System.out.println("nik2 !");
|
||||
}
|
||||
}
|
||||
}
|
||||
public class ClientWindow implements ActionListener {
|
||||
JFrame chatWindow;
|
||||
|
||||
JPanel chatPanel;
|
||||
JTextArea chatText;
|
||||
JScrollPane textScroll;
|
||||
|
||||
JPanel sendPanel;
|
||||
JTextField messageField;
|
||||
JButton sendButton;
|
||||
|
||||
String address;
|
||||
int sport;
|
||||
int dport;
|
||||
String username;
|
||||
|
||||
ClientWindow()
|
||||
{
|
||||
/* System.out.println("Connecting to server...");
|
||||
System.out.println("Establishing I/O streams...");*/
|
||||
chatWindow = new JFrame("Système de clavardage 2.0.1");
|
||||
|
||||
chatPanel = new JPanel(new GridLayout(2, 2));
|
||||
chatText = new JTextArea(1,1);
|
||||
textScroll = new JScrollPane(chatText);
|
||||
|
||||
sendPanel = new JPanel(new GridLayout(1, 2));
|
||||
messageField = new JTextField();
|
||||
sendButton = new JButton("Envoyer");
|
||||
sendButton.addActionListener(this);
|
||||
messageField.addActionListener(this);
|
||||
|
||||
|
||||
sendPanel.add(messageField);
|
||||
sendPanel.add(sendButton);
|
||||
|
||||
chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
chatWindow.setSize(new Dimension(1024, 768));
|
||||
|
||||
chatText.setLineWrap(true);
|
||||
chatText.setEditable(false);
|
||||
|
||||
|
||||
chatPanel.add(textScroll);
|
||||
chatPanel.add(sendPanel);
|
||||
|
||||
chatWindow.getContentPane().add(chatPanel, BorderLayout.CENTER);
|
||||
|
||||
|
||||
//Display the window.
|
||||
chatWindow.pack();
|
||||
chatWindow.setVisible(true);
|
||||
|
||||
try
|
||||
{
|
||||
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
|
||||
System.out.print("Entrez l'adresse de destination:");
|
||||
address = console.readLine();
|
||||
System.out.print("Entrez le numéro de port source:");
|
||||
sport = Integer.parseInt(console.readLine());
|
||||
System.out.print("Entrez le numéro de port destination:");
|
||||
dport = Integer.parseInt(console.readLine());
|
||||
System.out.print("Entrez votre nom d'utilisateur:");
|
||||
username = console.readLine();
|
||||
|
||||
ReceiveThread t2 = new ReceiveThread(sport, chatText);
|
||||
t2.start();
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
System.out.println("nik3!");
|
||||
}
|
||||
}
|
||||
public static void main (String [] args)
|
||||
{
|
||||
JFrame.setDefaultLookAndFeelDecorated(true);
|
||||
|
||||
ClientWindow window = new ClientWindow();
|
||||
}
|
||||
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
String message = messageField.getText();
|
||||
messageField.setText("");
|
||||
chatText.append(username + " : " + message + "\n");
|
||||
SendThread t1 = new SendThread(username, address, message, dport);
|
||||
t1.start();
|
||||
}
|
||||
}
|
1
src/chat/module-info.java
Normal file
1
src/chat/module-info.java
Normal file
|
@ -0,0 +1 @@
|
|||
package chat;
|
|
@ -1,116 +0,0 @@
|
|||
//package chat;
|
||||
import java.net.*;
|
||||
|
||||
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.net.DatagramSocket;
|
||||
|
||||
class SendThread extends Thread {
|
||||
String username;
|
||||
String address;
|
||||
String message;
|
||||
int port;
|
||||
|
||||
SendThread(String in_username, String in_address, String in_message, int in_port)
|
||||
{
|
||||
username = in_username;
|
||||
address = in_address;
|
||||
message = in_message;
|
||||
port = in_port;
|
||||
}
|
||||
public void run()
|
||||
{
|
||||
Socket link;
|
||||
Boolean connected = false;
|
||||
PrintWriter out;
|
||||
|
||||
while(!connected)
|
||||
{
|
||||
try
|
||||
{
|
||||
link = new Socket(address, port);
|
||||
out = new PrintWriter(link.getOutputStream(),true);
|
||||
out.println(username + ":" + message);
|
||||
|
||||
connected = true;
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
System.out.println("nik1!");
|
||||
connected = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class ReceiveThread extends Thread {
|
||||
|
||||
int port;
|
||||
ReceiveThread(int in_port)
|
||||
{
|
||||
port = in_port;
|
||||
}
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
ServerSocket servSocket = new ServerSocket(port);
|
||||
Boolean exit = false;
|
||||
|
||||
while(!exit)
|
||||
{
|
||||
Socket link = servSocket.accept();
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream()));
|
||||
String message = in.readLine();
|
||||
if(message == "exit")
|
||||
exit = true;
|
||||
else
|
||||
System.out.println(message);
|
||||
|
||||
link.close();
|
||||
}
|
||||
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
System.out.println("nik2 !");
|
||||
}
|
||||
}
|
||||
}
|
||||
public class socket_client {
|
||||
public static void main (String [] args)
|
||||
{
|
||||
/* System.out.println("Connecting to server...");
|
||||
System.out.println("Establishing I/O streams...");*/
|
||||
try
|
||||
{
|
||||
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
|
||||
System.out.print("Entrez l'adresse de destination:");
|
||||
String address = console.readLine();
|
||||
System.out.print("Entrez le numéro de port source:");
|
||||
int sport = Integer.parseInt(console.readLine());
|
||||
System.out.print("Entrez le numéro de port destination:");
|
||||
int dport = Integer.parseInt(console.readLine());
|
||||
System.out.print("Entrez votre nom d'utilisateur:");
|
||||
String username = console.readLine();
|
||||
SendThread t1;
|
||||
ReceiveThread t2 = new ReceiveThread(sport);
|
||||
t2.start();
|
||||
while(true)
|
||||
{
|
||||
String message = console.readLine();
|
||||
t1 = new SendThread(username, address, message, dport);
|
||||
t1.start();
|
||||
}
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
System.out.println("nik3!");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,2 +0,0 @@
|
|||
module chat {
|
||||
}
|
Loading…
Reference in a new issue