import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ChatForm extends JFrame implements KeyListener, WindowListener {
 private static final long serialVersionUID = 1L;
 private JTextArea content;
 private JTextField word;
 private JTextField name;
 private Alert al;
 Socket so;
 
rintStream ps;
 BufferedReader br;
 ChatForm() throws Exception {
  content = new JTextArea("", 20, 40);
  // 不可编辑
  content.setEnabled(false);
  // 可滚动
  JScrollPane jsp = new JScrollPane(content);
  word = new JTextField("", 40);
  // 添加事件
  word.addKeyListener(this);
  name = new JTextField("", 10);
  name.setEnabled(false);
  word.setEnabled(false);
  JPanel jp = new JPanel();
  this.setTitle("简单聊天室");
  jp.add(name);
  jp.add(word);
  this.addWindowListener(this);
  this.setLayout(new BorderLayout());
  this.add(jsp, BorderLayout.CENTER);
  this.add(jp, BorderLayout.SOUTH);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.pack();
  this.setVisible(true);
  startClient();
 }
 public void setUserName(String name){
  this.name.setText(name);
 }
 public void startClient() throws Exception {
  int i = 0;
  while (so == null) {
   i++;
   try {
    so = new Socket("192.168.0.123", 7456);
    al = new Alert(this,"",false);
    al.setMessage("请输入您的名字:");
    name.setEnabled(true);
    word.setEnabled(true);
   } catch (Exception e) {
    if(i>10){System.out.println("服务器还是没有开启,可能不会开了,我走了.");Thread.sleep(2000);System.exit(0);}
    content.setText("服务器未开启,程序10秒钟后重新连接,第"+i+"次尝试\r\n");
    Thread.sleep(10000);
    // System.exit(0);
   }
  }
  content.setText("连接到服务器咯\r\n别忘了给自己起个名字
");
  InputStream is = so.getInputStream();
  OutputStream os = so.getOutputStream();
  br = new BufferedReader(new InputStreamReader(is));
  ps = new PrintStream(os);
  Thread t = new Thread() {
   public void run() {
    String tmp = null;
    try {
     while ((tmp = br.readLine()) != null) {
      if(tmp.equalsIgnoreCase("bye")){
       content.setText( "服务器退了,我们也退咯"+"\r\n" +content.getText() );
       Thread.sleep(1000);
       close();
       
      }
      content.setText( tmp+"\r\n" +content.getText() );
     }
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  };
  t.start();
 }
 public static void main(String[] args) throws Exception {
  new ChatForm();
 }
 public void keyPressed(KeyEvent e) {
 }
 public void close() {
  if(ps!=null)ps.println("bye");
  try {
   if(br!=null)br.close();
   if(ps!=null)ps.close();
   if(so!=null)so.close();
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e1) {
    e1.printStackTrace();
   }
   System.exit(0);
  } catch (IOException e1) {
   e1.printStackTrace();
  }
 }
 public void sendMsg(String s) throws Exception {
  ps.println(s);
 }
 public void keyReleased(KeyEvent e) {
  int tmp = e.getKeyCode();
  if (KeyEvent.VK_ENTER == tmp) {
   String s = word.getText();
   String sname = name.getText();
   if (!s.equalsIgnoreCase("")) {
    if (s.equalsIgnoreCase("bye")) {
     close();
    }
    String stmp = sname + ":" + s;
    try {
     sendMsg(stmp);
    } catch (Exception e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
    }
    // content.setText(content.getText() + stmp+"\r\n");
    word.setText("");
   }
  }
 }
 public void keyTyped(KeyEvent e) {
 }
 public void windowActivated(WindowEvent e) {
 }
 public void windowClosed(WindowEvent e) {
 }
 public void windowClosing(WindowEvent e) {
  close();
 }
 public void windowDeactivated(WindowEvent e) {
 }
 public void windowDeiconified(WindowEvent e) {
 }
 public void windowIconified(WindowEvent e) {
 }
 public void windowOpened(WindowEvent e) {
 }
}