import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
public class ChatClient{
private boolean runnable = true;
public static void main
(String args
[]){
ChatClient cc = new ChatClient();
cc.createUI();
cc.inputName();
cc.connect();
cc.createThread();
}
public void createUI(){
jta.setEditable(false);
p.add(jtf,"Center");
p.add(send,"East");
jf.add(jta,"Center");
jf.add(p,"South");
MyClientListener listener = new MyClientListener(this);
send.addActionListener(listener);
jtf.addActionListener(listener);
ChatClient.this.shutDown();
}
});
jf.setSize(400,400);
jf.setLocation(600,0);
jf.setVisible(true);
jtf.requestFocus();
}
public void inputName(){
this.setName(name);
jf.setTitle(name);
}
public void connect(){
try{
s
= new Socket("127.0.0.1",
9999);
dos.writeUTF(name);
e.printStackTrace();
}
}
public void createThread(){
MyClientReader reader = new MyClientReader(this);
reader.start();
}
public void stop(){
runnable = false;
}
public void shutDown(){
try{
dos.writeUTF("Bye");
jta.append("Exit in 5 seconds");
this.stop();
dis.close();
dos.close();
s.close();
e.printStackTrace();
}
}
public boolean getRunnable(){
return runnable;
}
public void setName
(String name
){
this.name = name;
}
return dis;
}
return dos;
}
return jta;
}
return jtf;
}
}
private ChatClient client;
public MyClientListener(ChatClient client){
this.client = client;
}
try{
client.getDataOutputStream().writeUTF(info);
e1.printStackTrace();
}
if(info.equals("bye")){
client.shutDown();
}
jtf.setText("");
jtf.requestFocus();
}
}
class MyClientReader
extends Thread{
private ChatClient client;
public MyClientReader(ChatClient client){
this.client = client;
}
public void run(){
try{
while(client.getRunnable()){
info = dis.readUTF();
jta.append(info+"\n");
}
e.printStackTrace();
}
}
}