package com.szd.chat;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class ChatClient extends JFrame {
private JPanel contentPane;
private JTextField txtName;
private JTextField txtMsg;
JTextArea txtContent;
PrintStream ps;
BufferedReader br;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChatClient frame = new ChatClient();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ChatClient() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 10, 414, 212);
contentPane.add(scrollPane);
txtContent = new JTextArea();
scrollPane.setViewportView(txtContent);
JLabel lblName = new JLabel("昵称:");
lblName.setFont(new Font("新宋体", Font.PLAIN, 12));
lblName.setBounds(10, 237, 54, 15);
contentPane.add(lblName);
txtName = new JTextField();
txtName.setBounds(46, 234, 66, 21);
contentPane.add(txtName);
txtName.setColumns(10);
JLabel lblMsg = new JLabel("内容:");
lblMsg.setFont(new Font("新宋体", Font.PLAIN, 12));
lblMsg.setBounds(115, 237, 54, 15);
contentPane.add(lblMsg);
txtMsg = new JTextField();
txtMsg.setBounds(145, 234, 195, 21);
contentPane.add(txtMsg);
txtMsg.setColumns(10);
JButton btnSend = new JButton("发送");
/**
* 按钮点击事件
*/
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = txtName.getText();
String msg = txtMsg.getText();
/**
* 判断内容是否为空
*/
if (!"".equals(msg)) {
ps.println(name + ":" + msg);
txtMsg.setText("");
}
}
});
btnSend.setFont(new Font("新宋体", Font.PLAIN, 12));
btnSend.setBounds(342, 233, 82, 23);
contentPane.add(btnSend);
/**
* Socket
*/
try {
Socket socket = new Socket("192.168.14.38", 28888);
ps = new PrintStream(socket.getOutputStream());
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
/**
* 新开一个线程用来接收消息
*/
new GetMsgFromServerThread(br).start();
} catch (Exception e1) {
e1.printStackTrace();
}
}
/**
* 接收消息的线程
*
* @author Administrator
*
*/
class GetMsgFromServerThread extends Thread {
private BufferedReader br;
public GetMsgFromServerThread(BufferedReader br) {
this.br = br;
}
@Override
public void run() {
String str = "";
while (this.isAlive()) {
try {
str = br.readLine();
if (!"".equals(str)) {
// System.out.println(str);
txtContent.append(str + "\n\r");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}