package com.test;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.HashSet;
import java.util.Set;
import javax.swing.DefaultComboBoxModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class Face extends JFrame implements Runnable {
private JButton jbtSend, jbtView, jbtExit;
private JLabel label;
private Icon icon;
private JPanel jp1, jp2, jp3, jp;
private JTextArea jta1, jta2;
private JScrollPane jsp1, jsp2;
private JTextField txtF;
private JComboBox comboBoxType;
private JComboBox comboBoxUser;
private FlowLayout fl;
private String name, word;
Socket socket = null;
BufferedReader br = null;
// 放一个集合装载在线的用户
private Set onlineUser = new HashSet();
private void init() {
setTitle("聊天室");
jbtSend = new JButton("发送聊天信息");
jbtView = new JButton("查看聊天纪录");
jbtExit = new JButton("离开聊天室");
icon = new ImageIcon("46.gif");
label = new JLabel("说: ", icon, 0);
comboBoxType = new JComboBox();
comboBoxType.setModel(new DefaultComboBoxModel(new String[] { "群聊",
"私聊" }));
comboBoxType.addActionListener(new comboBoxAction());
comboBoxUser = new JComboBox();
comboBoxUser.setModel(new DefaultComboBoxModel(onlineUser.toArray()));
comboBoxUser.setVisible(false);
jta1 = new JTextArea(15, 35);
jta2 = new JTextArea(8, 35);
jsp1 = new JScrollPane(jta1);
jsp2 = new JScrollPane(jta2);
txtF = new JTextField(15);
jp = new JPanel();
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
fl = new FlowLayout();
}
private void addItems() {
setLayout(fl);
jp1.add(jsp1);
// jp2.setLayout(fl);
jp2.add(label);
jp2.add(txtF);
jp2.add(comboBoxType);
jp2.add(comboBoxUser);
jp.add(jbtSend);
jp.add(jbtView);
jp.add(jbtExit);
jp3.add(jsp2);
add(jp1);
add(jp2);
add(jp);
add(jp3);
}
private void addAction() {
Actions action = new Actions();
jbtSend.addActionListener(action);
jbtExit.addActionListener(action);
}
private void setFrame() {
setSize(400, 560);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width - getWidth()) / 2,
(screen.height - getHeight()) / 2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
sayHello();
}
public Face(String name) {
try {
this.name = name;
socket = new Socket("127.0.0.1", 8080);
init();
addItems();
addAction();
setFrame();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void sayHello() {
OutputStream os = null;
PrintWriter pw = null;
try {
int type = comboBoxType.getSelectedIndex();// 获得聊天方式
os = socket.getOutputStream();
pw = new PrintWriter(os);
// 发送的形式为: 昵称:方式:客户:内容
String message = name + ":" + type + ":" + "" + ":" + "登录了";
pw.println(message);
pw.flush();
InputStream is = socket.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
Thread t = new Thread(this);
t.start();
} catch (Exception ee) {
ee.printStackTrace();
}
}
public void send() {
OutputStream os = null;
PrintWriter pw = null;
try {
os = socket.getOutputStream();
pw = new PrintWriter(os);
InputStream is = socket.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
String str = txtF.getText();
txtF.setText("");
// 发送的形式为: 昵称:方式:客户:内容
int type = comboBoxType.getSelectedIndex();// 获得聊天方式
String user = "";
if (type == 1)// 私聊
user = (String) comboBoxUser.getSelectedItem();
pw.println(name + ": " + type + ":" + user + ":" + str);
pw.flush();
} catch (Exception ee) {
ee.printStackTrace();
}
}
public void run() {
try {
while (true) {
jta1.append("\n " + br.readLine());
}
} catch (Exception er) {
er.printStackTrace();
}
}
class Actions implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if (str.equals("离开聊天室")) {
System.exit(0);
} else if (str.equals("发送聊天信息")) {
if (!(txtF.getText().equals(""))) {
send();
}
}
}
}
class comboBoxAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
if (comboBoxType == e.getSource()) {
// 首先获得所有在线用户
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("c:/user.txt"));
onlineUser = (Set) ois.readObject();
ois.close();
comboBoxUser.setModel(new DefaultComboBoxModel(onlineUser
.toArray()));
comboBoxUser.setVisible(true);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
class Log extends JFrame {
private JLabel jlabel;
private JTextField jtxt;
private JButton jbt;
private FlowLayout fl;
private String name;
private void init() {
jlabel = new JLabel("请输入用户名:");
jtxt = new JTextField(20);
jbt = new JButton("确定");
fl = new FlowLayout();
}
private void addItems() {
setLayout(fl);
add(jlabel);
add(jtxt);
add(jbt);
}
private void addAction() {
jbt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jtxt.getText().equals("")) {
jlabel.setText("Please input your name!");
return;
}
name = jtxt.getText();
setVisible(false);
new Face(name);
}
});
}
private void setFrame() {
setSize(280, 120);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width - getWidth()) / 2,
(screen.height - getHeight()) / 2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public Log() {
init();
addItems();
addAction();
setFrame();
}
}
public class Client {
public static void main(String[] args) {
new Log();
}
}