Class chatjavaswing
package chat;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.Font;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class chatjavaswing {
private JFrame frame;
private JTextField mess;
static JTextArea arena;
static ServerSocket ss;
static Socket s;
static DataInputStream in;
static DataOutputStream out;
/**
* Launch the application.
*/
private void initialize() {
frame = new JFrame("people 1");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
arena = new JTextArea();
arena.setEditable(false);
arena.setBounds(0, 0, 434, 206);
frame.getContentPane().add(arena);
mess = new JTextField();
mess.setFont(new Font("Sylfaen", Font.PLAIN, 22));
mess.setBounds(0, 217, 260, 33);
frame.getContentPane().add(mess);
mess.setColumns(10);
JButton btnNewButton = new JButton("Sent");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String magout="";
magout=mess.getText().trim();
out.writeUTF(magout);
}catch (Exception ex) {
ex.printStackTrace();
}
}
});
btnNewButton.setBounds(286, 219, 89, 23);
frame.getContentPane().add(btnNewButton);
}
public chatjavaswing() {
initialize();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
chatjavaswing window = new chatjavaswing();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
String magin="";
try {
ss = new ServerSocket(52919);
s=ss.accept();
in=new DataInputStream(s.getInputStream());
out=new DataOutputStream(s.getOutputStream());
while(magin.equals("exit")) {
magin=in.readUTF();
arena.setText(arena.getText().trim()+"\n"+magin);
}
}catch (Exception ex) {
ex.printStackTrace();
}
}
}
class chatjavaswing1
package chat;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.Font;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class chatjavaswing1 {
private JFrame frame;
private JTextField mess;
static JTextArea arena;
static Socket s;
static DataInputStream in;
static DataOutputStream out;
private void initialize() {
frame = new JFrame("people 2");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
arena = new JTextArea();
arena.setEditable(false);
arena.setBounds(0, 0, 434, 206);
frame.getContentPane().add(arena);
mess = new JTextField();
mess.setFont(new Font("Sylfaen", Font.PLAIN, 22));
mess.setBounds(0, 217, 260, 33);
frame.getContentPane().add(mess);
mess.setColumns(10);
JButton btnNewButton = new JButton("Sent");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String magout="";
magout=mess.getText().trim();
out.writeUTF(magout);
}catch (Exception ex) {
ex.printStackTrace();
}
}
});
btnNewButton.setBounds(286, 219, 89, 23);
frame.getContentPane().add(btnNewButton);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
chatjavaswing1 window = new chatjavaswing1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
try {
s=new Socket("192.168.1.1",52919);
in=new DataInputStream(s.getInputStream());
out=new DataOutputStream(s.getOutputStream());
String magin="";
while(magin.equals("exit")) {
magin=in.readUTF();
arena.setText(arena.getText().trim()+"\n chatjavaswing:\t"+magin);
}
}catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Create the application.
*/
public chatjavaswing1() {
initialize();
}
}