正文

charroom客户端22006-06-14 09:49:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/sword2008/15801.html

分享到:

import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class charroom implements Runnable{
//----------------------------------------------------------****************定义变量
//JFrame
JFrame charFrame;
JTextArea charText;
JTextField charField1,charField2,charField3,charField4;
JButton charButton1,charButton2,charButton3;
ScrollPane sPanel;
JLabel charLabel;
//JPanel
JPanel jPanel1,jPanel2;
//Socket变量
Socket server,socket;
//文件,io
BufferedReader in;
PrintWriter out;
BufferedReader wt;
//Thread
Thread charThread=new Thread(this);


//_________________________________________________________________________________

public  charroom(){
 gUi();
 }

//------------------------------------------------------------***************方法
//客服端图形
void gUi(){
//new
 charFrame=new JFrame("聊天室客户端");
 jPanel1=new JPanel();
 jPanel2=new JPanel();
 charLabel=new JLabel("你对");
 charButton1=new JButton("连接");
 charButton2=new JButton("发送");
 charButton3=new JButton("退出");
 charText=new JTextArea(20,20);
 charText.setLineWrap(true);
 charText.setWrapStyleWord(true);
 sPanel=new ScrollPane();
 
 charField1=new JTextField(20);
 charField2=new JTextField(20);
 charField3=new JTextField("昵称",10);
 charField4=new JTextField("大家",10);
 Container chartain=charFrame.getContentPane();
//add
    jPanel1.add(charField3);
    jPanel1.add(charButton1);
    jPanel1.add(charField1);
    jPanel1.add(charButton3);
    sPanel.add(charText);
   
    jPanel2.add(charLabel);
    jPanel2.add(charField4);
    jPanel2.add(charField2);
    jPanel2.add(charButton2);
 chartain.add(jPanel1,BorderLayout.NORTH);
 chartain.add(sPanel,BorderLayout.CENTER);
 chartain.add(jPanel2,BorderLayout.SOUTH);
 
 charFrame.setBounds(200,200,500,400);
 charFrame.setVisible(true);
 charFrame.pack();
 charFrame.show();
//------------------------------------------------------------***********监听
 //发送
  charButton2.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    send();
   }
  });
 //连接
 charButton1.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    connect();
   }
  });
 //退出
 charButton1.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
   out();
  }
  });
 
//_________________________________________________________________________________
 
 }
//_________________
 //Run
public void run(){
try{while(true){

 String str=in.readLine();
    charText.append(str+"\n");
}
 
    }
catch(Exception e){
}   
    }
//联接connect()
public void connect(){
  try{
  server=new Socket(InetAddress.getByName(charField1.getText()).getHostAddress(),6666);
  if(server!=null){//connection successed
    charText.setText(charField3.getText()+"连接成功!");
   }

//    server=new Socket(InetAddress.getLocalHost(),6666);
  in=new BufferedReader(new InputStreamReader(server.getInputStream()));
  out=new PrintWriter(server.getOutputStream());
  wt=new BufferedReader(new InputStreamReader(System.in));
 
  charThread.start();
  }
  catch(Exception e){
  }
 
 
}
//发送send
void send(){
 out.println(charField3.getText()+"对大家说:"+charField2.getText());
 out.flush();
 charField2.setText("");
}
//退出方法
void out(){
 try
  { server.close();
    System.exit(0);}
  catch(Exception event){
   
  }
}


//_________________________________________________________________________________

 public static void main(String args[]){
  new charroom();
 }
}

阅读(442) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册