packagecom.chact;importjava.awt.BorderLayout;importjava.awt.Button;importjava.awt.Color;importjava.awt.Font;importjava.awt.Frame;importjava.awt.TextArea;importjava.awt.TextField;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.io.OutputStreamWriter;importjava.io.UnsupportedEncodingException;importjava.net.Socket;importjava.net.UnknownHostException;importjavax.swing.JOptionPane;importorg.json.JSONException;importorg.json.JSONObject;
@SuppressWarnings("serial")public class ChactClient extendsFrame {/*** 定义所有要使用的成员变量。
**/
public String Other = null;public Button mButton = new Button("使用名称");public static TextField mTextField = new TextField(20);public static TextField usernameTextField = new TextField(20);public static TextArea mTextArea = new TextArea(30, 30);public staticBoolean Flag;
Socket socket= null;static StringBuffer stringBuffer = newStringBuffer();public static Socket s = null;
OutputStream outputStream= null;
OutputStreamWriter writer= null;static String ChactContent = null;/***
* 定义的主方法
*
*@paramargs
*@throwsUnknownHostException
*@throwsIOException*/
public static void main(String[] args) throwsUnknownHostException, IOException {newChactClient().launchFrame();/*** 开启一个新的线程用来连接到服务器端*/Thread mThread= newThread() {
@Overridepublic voidrun() {try{
s= new Socket("123.206.76.188", 12345);
}catch(UnknownHostException e1) {//TODO Auto-generated catch block
e1.printStackTrace();
}catch(IOException e1) {//TODO Auto-generated catch block
e1.printStackTrace();
}/********************************************************************************/
/***
* 循环的从服务器读取数据
**/
while (true) {try{
InputStream iso=s.getInputStream();
InputStreamReader sior= new InputStreamReader(iso, "UTF-8");
BufferedReader bufferedReader= newBufferedReader(sior);
String string;while ((string = bufferedReader.readLine()) != null) {
System.out.println(string);//这里使用JSON数据格式进行数据的传递
JSONObject jsonObject= newJSONObject(string);//如果数据是自己发送的就不必再进行读取和显示
if (!jsonObject.getString("username").equals(usernameTextField.getText().trim())) {
mTextArea.append(jsonObject.getString("username") + ": "
+ jsonObject.getString("Content") + "\n");
mTextArea.setForeground(Color.CYAN);
stringBuffer= null;
}else{
stringBuffer= null;
}
}
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(JSONException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
mThread.start();
}/****************************************************************************/
/***
* 定义可见的窗口
*
*@throwsUnknownHostException
*@throwsIOException*/
public void launchFrame() throwsUnknownHostException, IOException {
Frame frame= newFrame();
frame.setTitle("开拓者即时聊天系统");
frame.setLocation(300, 300);//this.setBounds(300,300,300,300);
frame.setSize(600, 800);
frame.setResizable(false);
frame.add(mButton, BorderLayout.LINE_START);
frame.add(usernameTextField, BorderLayout.LINE_END);
frame.add(mTextArea, BorderLayout.NORTH);
frame.add(mTextField, BorderLayout.SOUTH);
mTextArea.setFont(new Font("微软雅黑", Font.PLAIN, 15));
frame.pack();
mTextField.addActionListener(newTFlistener());
mTextArea.setEditable(false);
mTextField.setEditable(false);
mButton.addActionListener(newActionListener() {/*** 首先进行用户名的读取如果为空则提示先输入用户名
*
**/@Overridepublic voidactionPerformed(ActionEvent e) {//TODO Auto-generated method stub//String Other;
Other =usernameTextField.getText().trim();
Other= Other.replace('\n', ' ');//System.out.println(Other);
if (!Other.equals("")) {
usernameTextField.setEditable(false);
mTextField.setEditable(true);
}else{
JOptionPane.showMessageDialog(null, "昵称不能为空!", "标题", JOptionPane.INFORMATION_MESSAGE);
}
}
});/***
* 定义关闭窗口的的事件监听器
**/frame.addWindowListener(newWindowAdapter() {
@Overridepublic voidwindowClosing(WindowEvent e) {//提示对方下线的JSON数据
String JSON=
"{" + "'username'" + ":" + Other + ',' +
"'Content'" + ":" + "已下线!!!" +
"}";try{
outputStream=socket.getOutputStream();
}catch(IOException e2) {//TODO Auto-generated catch block
e2.printStackTrace();
}try{
writer= new OutputStreamWriter(outputStream, "UTF-8");
}catch(UnsupportedEncodingException e2) {//TODO Auto-generated catch block
e2.printStackTrace();
}try{
writer.write(JSON+ "\n");
}catch(IOException e2) {//TODO Auto-generated catch block
e2.printStackTrace();
}try{
writer.flush();
}catch(IOException e2) {//TODO Auto-generated catch block
e2.printStackTrace();
}
System.exit(0);try{
socket.close();
destory();
}catch(IOException e1) {//TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
frame.setVisible(true);//获取与服务器的连接
Contection();
}/***
* 定义与服务器连接的函数
*
*@throwsUnknownHostException
*@throwsIOException*/
public void Contection() throwsUnknownHostException, IOException {
socket= new Socket("123.206.76.188", 12345);
System.out.println("成功连接到客户端");
}/***
* 监听回车键进行数据的发送的监听器
*
*
*@authorAdministrator-SYSTEM
**/
private class TFlistener implementsActionListener {
@Overridepublic voidactionPerformed(ActionEvent e) {
ChactContent=mTextField.getText().trim();try{
String JSON=
"{" + "'username'" + ":" + Other + ',' +
"'Content'" + ":" + ChactContent +
"}";
outputStream=socket.getOutputStream();
writer= new OutputStreamWriter(outputStream, "UTF-8");
writer.write(JSON+ "\n");
writer.flush();
mTextArea.setForeground(Color.BLACK);
mTextArea.append("我说: " + ChactContent + "\n");
mTextField.setText("");
}catch(IOException e1) {//TODO Auto-generated catch block
e1.printStackTrace();
}
}
}//进行资源的释放
public void destory() throwsIOException {
outputStream.close();
writer.close();
socket.close();
}
}