package net;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;
import java.awt.event.KeyEvent;
import javax.swing.KeyStroke;
import javax.swing.JComponent;
import java.net.ServerSocket;
import java.util.LinkedList;
import java.io.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Qq extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = -3859430711620083215L;
private static JTextArea textArea_1;
private static JTextArea textArea;
private JButton button;
private static JList list;
private JComboBox comboBox;
private static LinkedList<Socket> list_1;
private static LinkedList<ListenSocket> list_2;
private static ServerSocket ss = null;
private static Socket s = null;
private final JButton button_1;
private static int m;
private String msg = null;
private static DefaultListModel listmodel;
/**
* Launch the applicat
*
* @param args
*/
public static void main(String args[]) {
Qq frame = new Qq();
frame.setVisible(true);
// Thread rec=new Thread(frame);
// rec.start();
while (true) {// 监听客户连接
try {
s = ss.accept();
System.out.println("====="+s);
ListenSocket socket = new ListenSocket(textArea, s);
AddConnect(s, socket);
socket.start();
} catch (Exception e) {
}
}
}
/**
* Create the frame
*/
public Qq() {
super("学习交流聊天室");
getContentPane().setBackground(Color.GREEN);
getContentPane().setLayout(null);
setBounds(100, 100, 439, 490);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// this.setResizable(false);
list_1 = new LinkedList<Socket>();
final JLabel label = new JLabel();
label.setBounds(0, 0, 431, 59);
getContentPane().add(label);
comboBox = new JComboBox();
comboBox.setBounds(0, 406, 97, 29);
comboBox.addItem("all");
getContentPane().add(comboBox);
button = new JButton();
button.setText("发送");
button.setBounds(103, 406, 77, 29);
button.addActionListener(this);
button.registerKeyboardAction(this, KeyStroke.getKeyStroke(
KeyEvent.VK_ENTER, java.awt.event.InputEvent.CTRL_DOWN_MASK),
JComponent.WHEN_IN_FOCUSED_WINDOW);
getContentPane().add(button);
button_1 = new JButton();
button_1.setText("关闭");
button_1.addActionListener(this);
button_1.setBounds(199, 406, 77, 29);
getContentPane().add(button_1);
listmodel = new DefaultListModel();
list = new JList(listmodel);
JScrollPane scrol = new JScrollPane(list);
scrol.setBounds(282, 66, 152, 369);
getContentPane().add(scrol);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(0, 65, 276, 262);
getContentPane().add(scrollPane);
textArea = new JTextArea();// 聊天窗口
textArea.setLineWrap(true);
textArea.setEditable(false);
scrollPane.setViewportView(textArea);
final JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(0, 333, 276, 67);
getContentPane().add(scrollPane_1);
textArea_1 = new JTextArea();
textArea_1.setLineWrap(true);
scrollPane_1.setViewportView(textArea_1);
try {
ss = new ServerSocket(5000);
textArea.append("服务器启动\n");
} catch (Exception e) {
}
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try {
ss.close();
System.exit(0);
}
catch (Exception ee) {
}
}
});
}
public static int geti(String s) {
return listmodel.indexOf(s);
}
public static void sendtosomebody(int i, String msg) {
Socket s = (Socket) list_1.get(i);
try {
PrintWriter sos = new PrintWriter(s.getOutputStream());
sos.println(msg);
sos.flush();
} catch (Exception e) {
}
}
public void actionPerformed(ActionEvent e) {
Object soso = e.getSource();
if (soso == button) {
if (!textArea_1.getText().trim().equals("")) {
msg = textArea_1.getText();
sendtoall("系统消息:" + msg);
textArea.append("系统消息:" + msg + "\n");
textArea_1.setText("");
}
}
if (soso == button_1) {
try {
ss.close();
dispose();
}
catch (Exception ee) {
}
}
}
public static void sendtoall(String msg) {
if (list_1 != null) {
for (int i = 0; i < list_1.size(); i++) {
Socket s = (Socket) list_1.get(i);
try {
PrintWriter sos = new PrintWriter(s.getOutputStream());
sos.println(msg);
sos.flush();
} catch (Exception e) {
}
}
}
}
public static void updatename() {
if (listmodel.getSize() > 1) {
for (int i = 0; i < list_1.size() - 1; i++) {
Socket s = (Socket) list_1.get(i);
try {
PrintWriter sos = new PrintWriter(s.getOutputStream());
sos.println("listmodel.getElementAt(i).toString()"
+ listmodel.getElementAt(listmodel.getSize() - 1));
;
sos.flush();
} catch (Exception e) {
}
}
}
}
public static void sendallname() {
if (listmodel.getSize() >= 1) {
for (int i = 0; i < listmodel.getSize(); i++) {// 用户打开聊天框发送确保无重名
Socket s = (Socket) list_1.get(list_1.size() - 1);
try {
PrintWriter sos = new PrintWriter(s.getOutputStream());
sos.println("listmodel.getElementAt(i).toString()"
+ listmodel.getElementAt(i).toString());
sos.flush();// 必须有此句
} catch (Exception e) {
}
}
}
}
public static void deletename(String msg) {
int i = geti(msg);
Socket s = (Socket) list_1.remove(i);
listmodel.remove(i);
try {
s.close();
} catch (Exception e) {
}
// ListenSocket socket=(ListenSocket)list_2.remove(i);
}
public static void addname(String msg) {// 添加用户列表
listmodel.addElement(msg);
}
public static void AddConnect(Socket s, ListenSocket socket) {
if (list_1 == null) {
list_1 = new LinkedList<Socket>();
} else {
list_1.add(s);
}
if (list_2 == null) {
list_2 = new LinkedList<ListenSocket>();
} else {
list_2.add(socket);
}
}
}
class ListenSocket extends Thread {
private Socket s = null;
private ServerSocket ss = null;
private BufferedReader in = null;
private PrintWriter out = null;
private boolean go;
private JTextArea a1;
private String msg = "";
private int count;
public ListenSocket(JTextArea a1, Socket s) {
this.a1 = a1;
this.s = s;
this.go = true;
this.count = 0;
}
public void run() {// 监听客户发送信息
String msg = null;
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream());
} catch (Exception e) {
}
if (count == 0) {
Qq.sendallname();
}
while (go) {
try {
msg = in.readLine().toString();
System.out.println("====Run===="+msg);
count++;
if (count == 1) {
Qq.addname("※" + msg + "※");
Qq.sendtoall("【" + msg + "】" + "上线了!");
Qq.updatename();
a1.append("【" + msg + "】" + "上线了!\n");
} else {
if (msg
.indexOf("comboBox.getSelectedItem().equals(all)",
0) != -1) {
Qq.sendtoall(msg.substring(38));
a1.append(msg.substring(38) + '\n');
} else {
if (msg.indexOf("this.addWindowListener(new WindowAdapter())",0) != -1) {
int nam = msg.indexOf("※");
String name = msg.substring(nam);
Qq.deletename(name);
Qq.sendtoall(msg);
} else {
int i = msg
.indexOf("this.listmodel.addElement(msg.substring(36))");
int ii = msg.indexOf("对", i);
int j = msg
.indexOf("mn=listmodel.indexOf(kehu)", i);
int jj = msg.indexOf("说", j);
String s1 = msg.substring(0, i);
String s2 = msg.substring(ii + 1, j);
int mm = Qq.geti(s1);
int nn = Qq.geti(s2);
Qq.sendtosomebody(mm, msg.substring(0, i)
+ msg.substring(ii, j) + msg.substring(jj));
Qq.sendtosomebody(nn, msg.substring(0, i)
+ msg.substring(ii, j) + msg.substring(jj));
a1.append(msg.substring(0, i)
+ msg.substring(ii, j) + msg.substring(jj));
}
}
}
} catch (Exception e) {
}
}
}
}
===================================
package net;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.io.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.KeyEvent;
import javax.swing.KeyStroke;
import javax.swing.JComponent;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
public class Client extends JFrame implements ActionListener, Runnable,
ListSelectionListener {
private JTextField textField;
private DefaultListModel listmodel;
private JTextArea textArea_1;
private JTextArea textArea;
private final JLabel label_1;
private JList list;
private final JButton button;
private JComboBox comboBox;
private final JScrollPane scrollPane_2;
private final JButton button_1;
private Socket socket = null;
protected String username = new String("游客");
private BufferedReader in = null;
private PrintWriter out = null;
private final JButton button_2;
private String msg = null;
private boolean kesong = false;
/**
* Launch the applicat
*
* @param args
*/
/*
* public static void main(String args[]) { try { Qq frame = new Qq();
* frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }
*/
/**
* Create the frame
*/
public Client() {
super("学习交流聊天室");
getContentPane().setBackground(Color.GREEN);
getContentPane().setLayout(null);
setBounds(100, 100, 439, 490);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
final JLabel label = new JLabel();
label.setBounds(0, 0, 431, 59);
getContentPane().add(label);
comboBox = new JComboBox();
comboBox.setBounds(0, 406, 97, 29);
comboBox.addItem("all");
getContentPane().add(comboBox);
button = new JButton();
button.setText("发送");
button.addActionListener(this);
button.registerKeyboardAction(this, KeyStroke.getKeyStroke(
KeyEvent.VK_ENTER, java.awt.event.InputEvent.CTRL_DOWN_MASK),
JComponent.WHEN_IN_FOCUSED_WINDOW);
button.setBounds(103, 406, 77, 29);
getContentPane().add(button);
button_1 = new JButton();
button_1.setText("关闭");
button_1.addActionListener(this);
button_1.setBounds(199, 406, 77, 29);
getContentPane().add(button_1);
listmodel = new DefaultListModel();
list = new JList(listmodel);
list.addListSelectionListener(this);
scrollPane_2 = new JScrollPane(list);
scrollPane_2.setBounds(282, 66, 152, 262);
getContentPane().add(scrollPane_2);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(0, 65, 276, 262);
getContentPane().add(scrollPane);
textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setEditable(false);
scrollPane.setViewportView(textArea);
final JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(0, 333, 276, 67);
getContentPane().add(scrollPane_1);
textArea_1 = new JTextArea();
textArea_1.setLineWrap(true);
scrollPane_1.setViewportView(textArea_1);
label_1 = new JLabel();
label_1.setText("昵称");
label_1.setBounds(282, 336, 31, 15);
getContentPane().add(label_1);
textField = new JTextField();
textField.setBounds(306, 334, 85, 20);
getContentPane().add(textField);
button_2 = new JButton();
button_2.setText("确定");
button_2.addActionListener(this);
button_2.setBounds(282, 364, 77, 23);
getContentPane().add(button_2);
try {
socket = new Socket("127.0.0.1", 5000);
textArea.append("已连接服务器\n");
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
} catch (Exception e) {
System.out.println(e.toString());
}
Thread rec = new Thread(this);
rec.start();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (kesong) {
out.println("this.addWindowListener(new WindowAdapter()"
+ "※" + username + "※");
out.flush();
try {
socket.close();
} catch (Exception ee) {
}
System.exit(0);
} else {
try {
socket.close();
} catch (Exception ee) {
}
dispose();
}
}
});
}
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
if (list.getSelectedIndex() != -1) {
if (comboBox.getItemCount() > 1) {
comboBox.removeItemAt(1);
comboBox.addItem(list.getSelectedValue().toString());
comboBox.setSelectedIndex(1);
} else {
comboBox.addItem(list.getSelectedValue().toString());
comboBox.setSelectedIndex(1);
}
}
}
}
public boolean denglu(String s) {
String m = "※" + s + "※";
boolean go = true;
for (int i = 0; i < listmodel.getSize(); i++) {
if (listmodel.getElementAt(i).equals(m)) {
go = false;
}
}
return go;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button_2) {
if (!textField.getText().trim().equals("")
&& denglu(textField.getText().trim())) {
username = textField.getText();
kesong = true;
label_1.setVisible(false);
textField.setVisible(false);
button_2.setVisible(false);
scrollPane_2.setBounds(282, 66, 152, 369);
try {
out.println(username);
out.flush();
} catch (Exception ee) {
System.out.println(e.toString());
}
} else {
JOptionPane.showMessageDialog(null, "昵称不能为空或已有人使用此名字");
}
}
if (e.getSource() == button) {
if (kesong) {
if (!textArea_1.getText().trim().equals("")) {// 去掉空格不为空
try {
if (comboBox.getSelectedItem().equals("all")) {
msg = "comboBox.getSelectedItem().equals(all)"
+ "※" + username + "※" + "说:"
+ textArea_1.getText();
} else {
msg = "※"
+ username
+ "※"
+ "this.listmodel.addElement(msg.substring(36))对"
+ comboBox.getSelectedItem().toString()
+ "mn=listmodel.indexOf(kehu)说:"
+ textArea_1.getText();
}
out.println(msg);
textArea_1.setText("");
out.flush();
} catch (Exception ee) {
}
}
} else {
JOptionPane.showMessageDialog(null, "请确定昵称");
}
}
if (e.getSource() == button_1) {
if (kesong) {
out.println("this.addWindowListener(new WindowAdapter()" + "※"
+ username + "※");
out.flush();
try {
socket.close();
} catch (Exception ee) {
}
dispose();
} else {
try {
socket.close();
} catch (Exception ee) {
}
dispose();
}
}
}
public void run() {
String msg = null;
while (true) {
try {
msg = in.readLine();
if (msg.indexOf("listmodel.getElementAt(i).toString()", 0) != -1) {// 接收用户列表
this.listmodel.addElement(msg.substring(36));
} else {
if (msg.indexOf(
"this.addWindowListener(new WindowAdapter()", 0) != -1) {
int i = msg.indexOf("※");
String kehu = msg.substring(i);
int mn = listmodel.indexOf(kehu);// 查找下线客户并删除
listmodel.remove(mn);
if (comboBox.getItemCount() > 1) {
if (comboBox.getItemAt(1).equals(kehu)) {
comboBox.removeItemAt(1);
comboBox.setSelectedIndex(0);
}
}
textArea.append(kehu + "离开了聊天室\n");
} else {
textArea.append(msg + "\n");// 接受消息
}
}
} catch (Exception e) {
}
}
}
public static void main(String args[]) {
Client client = new Client();
client.setVisible(true);
}
}
可以看看