客户端代码
package l2;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
public class Cl implements Runnable {
Client5 frame = null;
String title = "";
TableDemo frames;
private static String localName = "";
private BufferedReader reader;
private PrintWriter writer;
private Socket socket;
private String toUser = "";
private HashMap map = new HashMap();
private Client5 checkMap(String read) {
if (read.startsWith("<send>")){
read = read.substring(read.indexOf("#") + 1, read
.indexOf("</sendTo>"));
if (!(map.containsKey(read.trim()))) {
frame = null;
frame = new Client5(read);
frame.setVisible(true);
map.put(read.trim(), frame);
}
return frame; }
else{
if (!(map.containsKey(read.trim()))) {
frame = null;
frame = new Client5(read);
frame.setVisible(true);
map.put(read.trim(), frame);
}
return frame;}
}
public PrintWriter getWriter() {
return writer;
}
public void setWriter(PrintWriter writer) {
this.writer = writer;
}
public void run() {
while (true) {
try {
String read = reader.readLine();
if (read == null || 0 == read.trim().length()) {
return;
}
/** <userList> 表示 用户列表 */
if (read.startsWith("<userList>"))
userList(read);
/** <sendTo> 表示 其他用户传来的消息 */
else if (read.startsWith("<sendTo>")) {
frame = checkMap(read);
frame.setVisible(true);
frame.getMsgView().append(read + ""n");
} else {
System.out.println("收到的消息与规则不匹配");
}
} catch (IOException ie) {
System.out.println("错误1 = " + ie);
}
}
}
private void connect() {
try {
System.err.println("尝试与服务器连接."n");
socket = new Socket("127.0.0.1", 7778);
localName = socket.getLocalAddress().toString()+socket.getLocalPort();
System.err.println("聊天准备完毕."n");
reader = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);
new Thread(this).start();
} catch (Exception e) {
System.err.println("连接失败..");
}
}
private void userList(String read) {
read = read.substring(10);
String[] reads = read.split("#");
String[][] data = new String[10][2];
for(int i = 0; i< reads.length ;i++){
System.out.println(reads[i]);
data[i][0] = reads[i];
}
// new 一个列表
String[] columnNames = { "在线用户名单" };
frames = new TableDemo(columnNames, data);
frames.pack();
frames.setVisible(true);
}
public static void main(String[] args) {
Cl client = new Cl();
client.connect();
}
/** 聊天框 面板类---------------------------- */
class Client5 extends Frame {
private TextField nameBox = new TextField(localName);
private TextArea msgView = new TextArea();
private TextField sendBox = new TextField();
public Client5() {
}
public Client5(String title) {
super(title);
msgView.setEditable(true);
add(nameBox, "North");
add(msgView, "Center");
add(sendBox, "South");
final String titles = title;
sendBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
String msg = "<sendTo>" + nameBox.getText() + "#"
+ titles + "</sendTo>" + sendBox.getText();
writer.println(msg);
System.out.println("msg = " + msg);
sendBox.setText("");
} catch (Exception ie) {
System.out.println("======聊天对话框=错误======= " + ie);
}
}
});
pack();
addWindowListener(new WindowAdapter() {// 注册窗口监听器
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public TextArea getMsgView() {
return msgView;
}
public void setMsgView(TextArea msgView) {
this.msgView = msgView;
}
}
/** 在线列表类-------------------------------------- */
class TableDemo extends JFrame {
private boolean DEBUG = true;
public TableDemo(String[] columnName, Object[][] data) { // 实现构造方法
super("RecorderOfWorkers"); // 首先调用父类JFrame的构造方法生成一个窗口
// MyTableModel myModel = new MyTableModel();//myModel存放表格的数据
MyTableModel myModel = new MyTableModel(columnName, data);// myModel存放表格的数据
JTable table = new JTable(myModel);// 表格对象table的数据来源是myModel对象
table.setPreferredScrollableViewportSize(new Dimension(180, 500));// 表格的显示尺寸
// 产生一个带滚动条的面板
JScrollPane scrollPane = new JScrollPane(table);
// 将带滚动条的面板添加入窗口中
this.setBounds(820, 15, 180, 600);
getContentPane().add(scrollPane, BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {// 注册窗口监听器
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
/** 内部类的内部类 */
// 把要显示在表格中的数据存入字符串数组和Object数组中
class MyTableModel extends AbstractTableModel {
// 表格中第一行所要显示的内容存放在字符串数组columnNames中
// final String[] columnNames = {"First Name",
// "Position",
// "Telephone",
// "MonthlyPay",
// "Married"};
// 表格中各行的内容保存在二维数组data中
final Object[][] data;
// {
// {"Wang Yi", "Executive",
// "01068790231", new Integer(5000), new Boolean(false)},
// {"Li Si", "Secretary",
// "01069785321", new Integer(3500), new Boolean(true)},
// {"Li Wu", "Manager",
// "01065498732", new Integer(4500), new Boolean(false)},
// {"Zhao Liu", "Safeguard",
// "01062796879", new Integer(2000), new Boolean(true)},
// {"Chen Qi", "Salesman",
// "01063541298", new Integer(4000), new Boolean(false)}
// };
final String[] columnNames;
public MyTableModel(String[] columnName, Object[][] datas) {
columnNames = columnName;
data = datas;
}
// 下述方法是重写AbstractTableModel中的方法,其主要用途是被JTable对象调用,///以便在表格中正确的显示出来。程序员必须根据采用的数据类型加以恰当实现。
// 获得列的数目
public int getColumnCount() {
return columnNames.length;
}
// 获得行的数目
public int getRowCount() {
return data.length;
}
// 获得某列的名字,而目前各列的名字保存在字符串数组columnNames中
public String getColumnName(int col) {
return columnNames[col];
}
// 获得某行某列的数据,而数据保存在对象数组data中
public Object getValueAt(int row, int col) {
return data[row][col];
}
// 判断每个单元格的类型
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
// 将表格声明为可编辑的
public boolean isCellEditable(int row, int col) {
return true;
}
// 改变某个数据的值
public void setValueAt(Object value, int row, int col) {
System.err.println("选择与 " + data[row][0] + "聊天");
checkMap((String) data[row][0]);
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i = 0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j = 0; j < numCols; j++) {
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}
}
}
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
public class Cl implements Runnable {
Client5 frame = null;
String title = "";
TableDemo frames;
private static String localName = "";
private BufferedReader reader;
private PrintWriter writer;
private Socket socket;
private String toUser = "";
private HashMap map = new HashMap();
private Client5 checkMap(String read) {
if (read.startsWith("<send>")){
read = read.substring(read.indexOf("#") + 1, read
.indexOf("</sendTo>"));
if (!(map.containsKey(read.trim()))) {
frame = null;
frame = new Client5(read);
frame.setVisible(true);
map.put(read.trim(), frame);
}
return frame; }
else{
if (!(map.containsKey(read.trim()))) {
frame = null;
frame = new Client5(read);
frame.setVisible(true);
map.put(read.trim(), frame);
}
return frame;}
}
public PrintWriter getWriter() {
return writer;
}
public void setWriter(PrintWriter writer) {
this.writer = writer;
}
public void run() {
while (true) {
try {
String read = reader.readLine();
if (read == null || 0 == read.trim().length()) {
return;
}
/** <userList> 表示 用户列表 */
if (read.startsWith("<userList>"))
userList(read);
/** <sendTo> 表示 其他用户传来的消息 */
else if (read.startsWith("<sendTo>")) {
frame = checkMap(read);
frame.setVisible(true);
frame.getMsgView().append(read + ""n");
} else {
System.out.println("收到的消息与规则不匹配");
}
} catch (IOException ie) {
System.out.println("错误1 = " + ie);
}
}
}
private void connect() {
try {
System.err.println("尝试与服务器连接."n");
socket = new Socket("127.0.0.1", 7778);
localName = socket.getLocalAddress().toString()+socket.getLocalPort();
System.err.println("聊天准备完毕."n");
reader = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);
new Thread(this).start();
} catch (Exception e) {
System.err.println("连接失败..");
}
}
private void userList(String read) {
read = read.substring(10);
String[] reads = read.split("#");
String[][] data = new String[10][2];
for(int i = 0; i< reads.length ;i++){
System.out.println(reads[i]);
data[i][0] = reads[i];
}
// new 一个列表
String[] columnNames = { "在线用户名单" };
frames = new TableDemo(columnNames, data);
frames.pack();
frames.setVisible(true);
}
public static void main(String[] args) {
Cl client = new Cl();
client.connect();
}
/** 聊天框 面板类---------------------------- */
class Client5 extends Frame {
private TextField nameBox = new TextField(localName);
private TextArea msgView = new TextArea();
private TextField sendBox = new TextField();
public Client5() {
}
public Client5(String title) {
super(title);
msgView.setEditable(true);
add(nameBox, "North");
add(msgView, "Center");
add(sendBox, "South");
final String titles = title;
sendBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
String msg = "<sendTo>" + nameBox.getText() + "#"
+ titles + "</sendTo>" + sendBox.getText();
writer.println(msg);
System.out.println("msg = " + msg);
sendBox.setText("");
} catch (Exception ie) {
System.out.println("======聊天对话框=错误======= " + ie);
}
}
});
pack();
addWindowListener(new WindowAdapter() {// 注册窗口监听器
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public TextArea getMsgView() {
return msgView;
}
public void setMsgView(TextArea msgView) {
this.msgView = msgView;
}
}
/** 在线列表类-------------------------------------- */
class TableDemo extends JFrame {
private boolean DEBUG = true;
public TableDemo(String[] columnName, Object[][] data) { // 实现构造方法
super("RecorderOfWorkers"); // 首先调用父类JFrame的构造方法生成一个窗口
// MyTableModel myModel = new MyTableModel();//myModel存放表格的数据
MyTableModel myModel = new MyTableModel(columnName, data);// myModel存放表格的数据
JTable table = new JTable(myModel);// 表格对象table的数据来源是myModel对象
table.setPreferredScrollableViewportSize(new Dimension(180, 500));// 表格的显示尺寸
// 产生一个带滚动条的面板
JScrollPane scrollPane = new JScrollPane(table);
// 将带滚动条的面板添加入窗口中
this.setBounds(820, 15, 180, 600);
getContentPane().add(scrollPane, BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {// 注册窗口监听器
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
/** 内部类的内部类 */
// 把要显示在表格中的数据存入字符串数组和Object数组中
class MyTableModel extends AbstractTableModel {
// 表格中第一行所要显示的内容存放在字符串数组columnNames中
// final String[] columnNames = {"First Name",
// "Position",
// "Telephone",
// "MonthlyPay",
// "Married"};
// 表格中各行的内容保存在二维数组data中
final Object[][] data;
// {
// {"Wang Yi", "Executive",
// "01068790231", new Integer(5000), new Boolean(false)},
// {"Li Si", "Secretary",
// "01069785321", new Integer(3500), new Boolean(true)},
// {"Li Wu", "Manager",
// "01065498732", new Integer(4500), new Boolean(false)},
// {"Zhao Liu", "Safeguard",
// "01062796879", new Integer(2000), new Boolean(true)},
// {"Chen Qi", "Salesman",
// "01063541298", new Integer(4000), new Boolean(false)}
// };
final String[] columnNames;
public MyTableModel(String[] columnName, Object[][] datas) {
columnNames = columnName;
data = datas;
}
// 下述方法是重写AbstractTableModel中的方法,其主要用途是被JTable对象调用,///以便在表格中正确的显示出来。程序员必须根据采用的数据类型加以恰当实现。
// 获得列的数目
public int getColumnCount() {
return columnNames.length;
}
// 获得行的数目
public int getRowCount() {
return data.length;
}
// 获得某列的名字,而目前各列的名字保存在字符串数组columnNames中
public String getColumnName(int col) {
return columnNames[col];
}
// 获得某行某列的数据,而数据保存在对象数组data中
public Object getValueAt(int row, int col) {
return data[row][col];
}
// 判断每个单元格的类型
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
// 将表格声明为可编辑的
public boolean isCellEditable(int row, int col) {
return true;
}
// 改变某个数据的值
public void setValueAt(Object value, int row, int col) {
System.err.println("选择与 " + data[row][0] + "聊天");
checkMap((String) data[row][0]);
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i = 0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j = 0; j < numCols; j++) {
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}
}
}