您的位置:首页 > 编程语言 > Java开发

局域网间的点对点聊天

2014-06-29 10:25 239 查看
聊天已经成为我们日常生活的一部分,各种聊天的软件已经飞满了我们的世界。今天就让我们来探讨一下局域网间的点对点的聊天吧。

我们在Eclipse中创建我们的工程。



其中带有Frame名字字样的工程是有界面的。如何在Eclipse中创建有界面的工程呢?我使用的VE插件,具体怎么用,问问度娘吧。

ServerThread:

package com.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.DefaultListModel;

/*
* ServerThread
* 主要工作:
* 1.等待客户端连接
* 2.接收客户端信息
* 3.接收到客户端信息后,
*   将信息发送至ChatFrame窗口
*/
public class ServerThread implements Runnable {

private ServerSocket server;
private ChatFrame chatFrame;
private String myName;
private String friendName;
public ServerThread(ServerSocket server,ChatFrame chatFrame,String myName){
this.server=server;
this.chatFrame=chatFrame;
this.myName=myName;
}
@Override
public void run() {
//等待客户连接
//1.更新窗体内容
String txt=this.chatFrame.getTxtpanMsgs().getText();
txt+="正在等待客户端连接……\n";
this.chatFrame.getTxtpanMsgs().setText(txt);
//2.等该客户端连接
try {
Socket client=server.accept();
//获取输入、输出流
BufferedReader in =new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out=new PrintWriter(client.getOutputStream());
//将输出流对象给ChatFrame
this.chatFrame.setOut(out);
//握手
//1.先接收客户端的名字
friendName=in.readLine();
//加一步,更新好友列表
DefaultListModel model=(DefaultListModel)this.chatFrame.getListFriends().getModel();
model.addElement("全部好友:");
model.addElement(friendName);
//2.向客户端发送我的名字:
out.println(myName);
out.flush ();
txt=this.chatFrame.getTxtpanMsgs().getText();
txt+="与好友<"+friendName+">已经建立连接,"+"可以开始聊天了……\n";
this.chatFrame.getTxtpanMsgs().setText(txt);
//循环获取客户端信息
do{
String msg=in.readLine();
txt=this.chatFrame.getTxtpanMsgs().getText();
txt+=msg+"\n";
this.chatFrame.getTxtpanMsgs().setText(txt);
}while(true);
} catch (IOException e) {
//网络问题,或者对方下线
}

}

}
ServerLoginFrame:

package com.server;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ServerSocket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class ServerLoginFrame extends JFrame implements  ActionListener{

private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JLabel labMsg = null;
private JButton butLogin = null;
private JButton butClose = null;
private JTextField txtPort = null;
private JTextField txtUserName = null;
/**
* This method initializes butLogin
*
* @return javax.swing.JButton
*/
private JButton getButLogin() {
if (butLogin == null) {
butLogin = new JButton();
butLogin.setBounds(new Rectangle(180, 160, 107, 29));
butLogin.setText("确定");
butLogin.addActionListener(this);
}
return butLogin;
}

/**
* This method initializes butClose
*
* @return javax.swing.JButton
*/
private JButton getButClose() {
if (butClose == null) {
butClose = new JButton();
butClose.setBounds(new Rectangle(20, 161, 107, 29));
butClose.setText("关闭");
butClose.addActionListener(this);
}
return butClose;
}

/**
* This method initializes txtPort
*
* @return javax.swing.JTextField
*/
private JTextField getTxtPort() {
if (txtPort == null) {
txtPort = new JTextField();
txtPort.setBounds(new Rectangle(128, 33, 159, 28));
}
return txtPort;
}

/**
* This method initializes txtUserName
*
* @return javax.swing.JTextField
*/
private JTextField getTxtUserName() {
if (txtUserName == null) {
txtUserName = new JTextField();
txtUserName.setBounds(new Rectangle(128, 78, 159, 28));
}
return txtUserName;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ServerLoginFrame thisClass = new ServerLoginFrame();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}

/**
* This is the default constructor
*/
public ServerLoginFrame() {
super();
initialize();
//将当前窗体显示在屏幕中央
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension size=kit.getScreenSize();
int screenWidth=size.width ;
int screenHeight=size.height ;
this.setLocation(screenWidth/2-this.getWidth()/2,screenHeight/2-this.getHeight()/2);
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(323, 235);
this.setContentPane(getJContentPane());
this.setResizable(false);
this.setTitle("启动服务器");
}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
labMsg = new JLabel();
labMsg.setBounds(new Rectangle(127, 122, 161, 24));
labMsg.setFont(new Font("Dialog", Font.BOLD, 12));
labMsg.setForeground(new Color(255, 7, 5));
labMsg.setText("");
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(15, 77, 112, 28));
jLabel1.setText("请输入您的昵称:");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(16, 33, 108, 28));
jLabel.setText("请指定端口号:");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(jLabel1, null);
jContentPane.add(labMsg, null);
jContentPane.add(getButLogin(), null);
jContentPane.add(getButClose(), null);
jContentPane.add(getTxtPort(), null);
jContentPane.add(getTxtUserName(), null);
}
return jContentPane;
}

@Override
public void actionPerformed(ActionEvent e) {

if(e.getSource()==this.butClose){
//按了关闭按钮
System.exit(0);//结束应用程序
}
if (e.getSource()==this.butLogin){
//按了确定按钮
//从txtPort文本框中获取文本
String port=this.txtPort.getText ();
//从txtUserName文本框中获取文本
String userName=this.txtUserName.getText();

//验证端口号
//1.验证每一位是否为数字
for(int i=0;i<port.length();i++){
char c=port.charAt(i);
if (c<'0'||c>'9'){
this.labMsg.setText("端口号应全部为数字");
return;
}
}
//2.验证是否是1——5位的长度
if(port.length()<1||port.length()>5){
this.labMsg.setText("端口号应为1——5位数字!");
return ;
}
//3.转换为int,判断是否大于65535
int intPort=Integer.parseInt(port);
if (intPort<1||intPort >65535){
this.labMsg.setText("端口号应为1——65535");
return;
}
//4.验证第一个字符不能为0
char c=port.charAt(0);
if (c=='0'){
this.labMsg.setText("第一个数字不能为0");
return;
}

//实例化ServerSocket
try {
ServerSocket server=new ServerSocket (intPort);
//1.先显示聊天窗体
ChatFrame chatFrame=new ChatFrame(userName);
chatFrame.setVisible(true);
//2.启动等待线程,等待客户端连接
ServerThread t=new ServerThread(server,chatFrame,userName);
new Thread(t).start();
//3.将自己销毁
this.dispose();
} catch (IOException e1) {
this.labMsg.setText("端口已被占用");
return;
}

}
}

}  //  @jve:decl-index=0:visual-constraint="118,22"
ChatFrame:

package com.server;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

public class ChatFrame extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JPanel jPanel1 = null;
private JPanel jPanel3 = null;
private JPanel jPanel4 = null;
private JList listFriends = null;
private JPanel jPanel2 = null;
private JTextPane txtpanMsgs = null;
private JPanel jPanel5 = null;
private JPanel jPanel6 = null;
private JTextArea txtMsg = null;
private JButton butSend = null;
private JButton butClose = null;
private JPanel jPanel7 = null;
private JPanel jPanel8 = null;
/**
* This is the default constructor
*/

//用于向客户端发送消息
private PrintWriter out=null;
private String myName;

public ChatFrame(String myName) {
super();
initialize();
this.myName=myName;

//将当前窗体显示在屏幕中央
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension size=kit.getScreenSize();
int screenWidth=size.width ;
int screenHeight=size.height ;
this.setLocation(screenWidth/2-this.getWidth()/2,screenHeight/2-this.getHeight()/2);
}
//设置PrintWriter对象
public void setOut(PrintWriter out){
this.out=out;
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(600, 429);
this.setContentPane(getJContentPane());
this.setTitle("聊天");
}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
BorderLayout borderLayout = new BorderLayout();
borderLayout.setHgap(10);
borderLayout.setVgap(10);
jContentPane = new JPanel();
jContentPane.setLayout(borderLayout);
jContentPane.add(getJPanel(), BorderLayout.NORTH);
jContentPane.add(getJPanel1(), BorderLayout.WEST);
jContentPane.add(getJPanel3(), BorderLayout.EAST);
jContentPane.add(getJPanel4(), BorderLayout.CENTER);
}
return jContentPane;
}

/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.setPreferredSize(new Dimension(0, 30));
jPanel.setBackground(new Color(60, 238, 238));
}
return jPanel;
}

/**
* This method initializes jPanel1
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel1() {
if (jPanel1 == null) {
jPanel1 = new JPanel();
jPanel1.setLayout(new GridBagLayout());
jPanel1.setPreferredSize(new Dimension(10, 0));
jPanel1.setBackground(new Color(238, 41, 206));
}
return jPanel1;
}

/**
* This method initializes jPanel3
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel3() {
if (jPanel3 == null) {
jPanel3 = new JPanel();
jPanel3.setLayout(new BorderLayout());
jPanel3.setPreferredSize(new Dimension(200, 0));
jPanel3.setBackground(new Color(238, 200, 66));
jPanel3.add(getListFriends(), BorderLayout.CENTER);
}
return jPanel3;
}

/**
* This method initializes jPanel4
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel4() {
if (jPanel4 == null) {
jPanel4 = new JPanel();
jPanel4.setLayout(new BorderLayout());
jPanel4.setBackground(new Color(130, 217, 24));
jPanel4.add(getJPanel2(), BorderLayout.SOUTH);
JScrollPane js =new JScrollPane(getTxtpanMsgs());
jPanel4.add(js, BorderLayout.CENTER);
}
return jPanel4;
}

/**
* This method initializes listFriends
*
* @return javax.swing.JList
*/
public JList getListFriends() {
if (listFriends == null) {
listFriends = new JList();
//DefaultListModel管理JList 中的内容
DefaultListModel model=new DefaultListModel();
listFriends.setModel(model);
}
return listFriends;
}

/**
* This method initializes jPanel2
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel2() {
if (jPanel2 == null) {
jPanel2 = new JPanel();
jPanel2.setLayout(new BorderLayout());
jPanel2.setPreferredSize(new Dimension(0, 150));
jPanel2.setBackground(new Color(23, 37, 137));
jPanel2.add(getJPanel5(), BorderLayout.NORTH);
jPanel2.add(getJPanel6(), BorderLayout.SOUTH);
JScrollPane js=new JScrollPane(this.getTxtMsg());
jPanel2.add(js, BorderLayout.CENTER);
}
return jPanel2;
}

/**
* This method initializes txtpanMsgs
*
* @return javax.swing.JTextPane
*/
public JTextPane getTxtpanMsgs() {
if (txtpanMsgs == null) {
txtpanMsgs = new JTextPane();
txtpanMsgs.setEditable(false);
}
return txtpanMsgs;
}

/**
* This method initializes jPanel5
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel5() {
if (jPanel5 == null) {
jPanel5 = new JPanel();
jPanel5.setLayout(new GridBagLayout());
jPanel5.setPreferredSize(new Dimension(0, 30));
}
return jPanel5;
}

/**
* This method initializes jPanel6
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel6() {
if (jPanel6 == null) {
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(1);
gridLayout.setHgap(10);
gridLayout.setVgap(10);
gridLayout.setColumns(4);
jPanel6 = new JPanel();
jPanel6.setLayout(gridLayout);
jPanel6.setPreferredSize(new Dimension(0, 30));
jPanel6.add(getJPanel7(), null);
jPanel6.add(getJPanel8(), null);
jPanel6.add(getButClose(), null);
jPanel6.add(getButSend(), null);
}
return jPanel6;
}

/**
* This method initializes txtMsg
*
* @return javax.swing.JTextArea
*/
private JTextArea getTxtMsg() {
if (txtMsg == null) {
txtMsg = new JTextArea();
}
return txtMsg;
}

/**
* This method initializes butSend
*
* @return javax.swing.JButton
*/
private JButton getButSend() {
if (butSend == null) {
butSend = new JButton();
butSend.setText("发送");
butSend.addActionListener(this);
}
return butSend;
}

/**
* This method initializes butClose
*
* @return javax.swing.JButton
*/
private JButton getButClose() {
if (butClose == null) {
butClose = new JButton();
butClose.setText("关闭");
butClose.addActionListener(this);
}
return butClose;
}

/**
* This method initializes jPanel7
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel7() {
if (jPanel7 == null) {
jPanel7 = new JPanel();
jPanel7.setLayout(new GridBagLayout());
}
return jPanel7;
}

/**
* This method initializes jPanel8
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel8() {
if (jPanel8 == null) {
jPanel8 = new JPanel();
jPanel8.setLayout(new GridBagLayout());
}
return jPanel8;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==this.butClose){

}
if (e.getSource()==this.butSend){
if (out==null){
return;
}
//向客户端发出信息
String msg=this.txtMsg.getText();
msg="<"+myName+"说:>"+msg;
out.println(msg);
out.flush();
//更新自己的聊天信息
String txt=this.getTxtpanMsgs().getText();
txt+=msg+"\n";
this.txtpanMsgs.setText(txt);
this.txtMsg.setText("");
}

}

}  //  @jve:decl-index=0:visual-constraint="10,10"


ClientThread:

package com.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class ClientThread implements Runnable {
private Socket client;
private String myName;
private BufferedReader in=null;
private ClientChatFrame chatFrame;

public ClientThread(Socket client,String myName,ClientChatFrame chatFrame){
this.client=client;
this.myName=myName;
this.chatFrame=chatFrame;
try {
this.in=new BufferedReader(new InputStreamReader(client.getInputStream()));
} catch (IOException e) {

}
}
@Override
public void run() {
//循环,获取服务器端信息
try {
do{
String msg=in.readLine();
//更新聊天记录
String txt=this.chatFrame.getTxtpanMsgs().getText();
txt+=msg+"\n";
this.chatFrame.getTxtpanMsgs().setText(txt);

}while(true);
} catch (IOException e) {

System.out.print("断线了,需要重新连接!");
}

}

}

ClientLoginFrame:

package com.client;

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import java.awt.Font;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;

public class ClientLoginFrame extends JFrame implements  ActionListener{

private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JLabel labMsg = null;
private JButton butLogin = null;
private JButton butClose = null;
private JTextField txtPort = null;
private JTextField txtUserName = null;
private JLabel jLabel2 = null;
private JTextField txtIP = null;
/**
* This method initializes butLogin
*
* @return javax.swing.JButton
*/
private JButton getButLogin() {
if (butLogin == null) {
butLogin = new JButton();
butLogin.setBounds(new Rectangle(186, 181, 107, 29));
butLogin.setText("确定");
butLogin.addActionListener(this);
}
return butLogin;
}

/**
* This method initializes butClose
*
* @return javax.swing.JButton
*/
private JButton getButClose() {
if (butClose == null) {
butClose = new JButton();
butClose.setBounds(new Rectangle(26, 182, 107, 29));
butClose.setText("关闭");
butClose.addActionListener(this);
}
return butClose;
}

/**
* This method initializes txtPort
*
* @return javax.swing.JTextField
*/
private JTextField getTxtPort() {
if (txtPort == null) {
txtPort = new JTextField();
txtPort.setBounds(new Rectangle(134, 58, 159, 28));
}
return txtPort;
}

/**
* This method initializes txtUserName
*
* @return javax.swing.JTextField
*/
private JTextField getTxtUserName() {
if (txtUserName == null) {
txtUserName = new JTextField();
txtUserName.setBounds(new Rectangle(134, 98, 159, 28));
}
return txtUserName;
}

/**
* This method initializes txtIP
*
* @return javax.swing.JTextField
*/
private JTextField getTxtIP() {
if (txtIP == null) {
txtIP = new JTextField();
txtIP.setBounds(new Rectangle(134, 18, 159, 28));
}
return txtIP;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ClientLoginFrame thisClass = new ClientLoginFrame();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}

/**
* This is the default constructor
*/
public ClientLoginFrame() {
super();
initialize();
//将当前窗体显示在屏幕中央
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension size=kit.getScreenSize();
int screenWidth=size.width ;
int screenHeight=size.height ;
this.setLocation(screenWidth/2-this.getWidth()/2,screenHeight/2-this.getHeight()/2);
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(306, 252);
this.setContentPane(getJContentPane());
this.setResizable(false);
this.setTitle("启动客户端");
}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(23, 17, 107, 29));
jLabel2.setText("请指定服务器IP:");
labMsg = new JLabel();
labMsg.setBounds(new Rectangle(133, 143, 161, 24));
labMsg.setFont(new Font("Dialog", Font.BOLD, 12));
labMsg.setForeground(new Color(255, 7, 5));
labMsg.setText("");
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(21, 98, 112, 28));
jLabel1.setText("请输入您的昵称:");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(3, 58, 133, 28));
jLabel.setText("请指定服务器端口号:");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(jLabel1, null);
jContentPane.add(labMsg, null);
jContentPane.add(getButLogin(), null);
jContentPane.add(getButClose(), null);
jContentPane.add(getTxtPort(), null);
jContentPane.add(getTxtUserName(), null);
jContentPane.add(jLabel2, null);
jContentPane.add(getTxtIP(), null);
}
return jContentPane;
}

@Override
public void actionPerformed(ActionEvent e) {

if(e.getSource()==this.butClose){
//按了关闭按钮
System.exit(0);//结束应用程序
}
if (e.getSource()==this.butLogin){
//按了确定按钮
//获取IP
String ip=this.txtIP.getText();
//从txtPort文本框中获取文本
String port=this.txtPort.getText ();
//从txtUserName文本框中获取文本
String userName=this.txtUserName.getText();

//验证端口号
//1.验证每一位是否为数字
for(int i=0;i<port.length();i++){
char c=port.charAt(i);
if (c<'0'||c>'9'){
this.labMsg.setText("端口号应全部为数字");
return;
}
}
//2.验证是否是1——5位的长度
if(port.length()<1||port.length()>5){
this.labMsg.setText("端口号应为1——5位数字!");
return ;
}
//3.转换为int,判断是否大于65535
int intPort=Integer.parseInt(port);
if (intPort<1||intPort >65535){
this.labMsg.setText("端口号应为1——65535");
return;
}
//4.验证第一个字符不能为0
char c=port.charAt(0);
if (c=='0'){
this.labMsg.setText("第一个数字不能为0");
return;
}

//实例化ServerSocket
try {
Socket client=new Socket (ip,intPort);

//1.先显示聊天窗体
ClientChatFrame chatFrame=new ClientChatFrame(client,userName);
chatFrame.setVisible(true);
//2.启动等待线程,等待客户端连接
ClientThread t=new ClientThread(client,userName,chatFrame);
new Thread(t).start();

//3.将自己销毁
this.dispose();
} catch (IOException e1) {
this.labMsg.setText("端口已被占用");
return;
}

}
}

}  //  @jve:decl-index=0:visual-constraint="118,22"

ClientCharFrame:

package com.client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
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.PrintWriter;
import java.net.Socket;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

public class ClientChatFrame extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JPanel jPanel1 = null;
private JPanel jPanel3 = null;
private JPanel jPanel4 = null;
private JList listFriends = null;
private JPanel jPanel2 = null;
private JTextPane txtpanMsgs = null;
private JPanel jPanel5 = null;
private JPanel jPanel6 = null;
private JTextArea txtMsg = null;
private JButton butSend = null;
private JButton butClose = null;
private JPanel jPanel7 = null;
private JPanel jPanel8 = null;

private Socket client =null;
private String myName;
private PrintWriter out=null;
private BufferedReader in =null;
private String friendName=null;
/**
* This is the default constructor
*/
public ClientChatFrame(Socket client,String myName) {
super();
initialize();
//将当前窗体显示在屏幕中央
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension size=kit.getScreenSize();
int screenWidth=size.width ;
int screenHeight=size.height ;
this.setLocation(screenWidth/2-this.getWidth()/2,screenHeight/2-this.getHeight()/2);

this.client=client;
this.myName=myName;
try {
this.out=new PrintWriter(client.getOutputStream());
out.println(this.myName );
out.flush();
this.in =new BufferedReader(new InputStreamReader(client.getInputStream()));
//接收服务器的名字
this.friendName =in.readLine();
//1.更新好友列表
DefaultListModel m=(DefaultListModel)this.getListFriends().getModel();
m.addElement("全部好友:");
m.addElement(friendName);
//2.更新聊天记录
String txt=this.getTxtpanMsgs().getText ();
txt+=" "+friendName+ "已登录,可以开始聊天了……\n";
this.getTxtpanMsgs().setText(txt);

} catch (IOException e) {

}
//发送第一次握手信息

}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(600, 429);
this.setContentPane(getJContentPane());
this.setTitle("聊天");
}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
BorderLayout borderLayout = new BorderLayout();
borderLayout.setHgap(10);
borderLayout.setVgap(10);
jContentPane = new JPanel();
jContentPane.setLayout(borderLayout);
jContentPane.add(getJPanel(), BorderLayout.NORTH);
jContentPane.add(getJPanel1(), BorderLayout.WEST);
jContentPane.add(getJPanel3(), BorderLayout.EAST);
jContentPane.add(getJPanel4(), BorderLayout.CENTER);
}
return jContentPane;
}

/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.setPreferredSize(new Dimension(0, 30));
jPanel.setBackground(new Color(60, 238, 238));
}
return jPanel;
}

/**
* This method initializes jPanel1
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel1() {
if (jPanel1 == null) {
jPanel1 = new JPanel();
jPanel1.setLayout(new GridBagLayout());
jPanel1.setPreferredSize(new Dimension(10, 0));
jPanel1.setBackground(new Color(238, 41, 206));
}
return jPanel1;
}

/**
* This method initializes jPanel3
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel3() {
if (jPanel3 == null) {
jPanel3 = new JPanel();
jPanel3.setLayout(new BorderLayout());
jPanel3.setPreferredSize(new Dimension(200, 0));
jPanel3.setBackground(new Color(238, 200, 66));
jPanel3.add(getListFriends(), BorderLayout.CENTER);
}
return jPanel3;
}

/**
* This method initializes jPanel4
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel4() {
if (jPanel4 == null) {
jPanel4 = new JPanel();
jPanel4.setLayout(new BorderLayout());
jPanel4.setBackground(new Color(130, 217, 24));
jPanel4.add(getJPanel2(), BorderLayout.SOUTH);
JScrollPane js=new JScrollPane(getTxtpanMsgs());
jPanel4.add(js, BorderLayout.CENTER);
}
return jPanel4;
}

/**
* This method initializes listFriends
*
* @return javax.swing.JList
*/
private JList getListFriends() {
if (listFriends == null) {
listFriends = new JList();
listFriends.setModel(new DefaultListModel());
}
return listFriends;
}

/**
* This method initializes jPanel2
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel2() {
if (jPanel2 == null) {
jPanel2 = new JPanel();
jPanel2.setLayout(new BorderLayout());
jPanel2.setPreferredSize(new Dimension(0, 150));
jPanel2.setBackground(new Color(23, 37, 137));
jPanel2.add(getJPanel5(), BorderLayout.NORTH);
jPanel2.add(getJPanel6(), BorderLayout.SOUTH);
JScrollPane js=new JScrollPane(this.getTxtMsg());
jPanel2.add(js, BorderLayout.CENTER);
}
return jPanel2;
}

/**
* This method initializes txtpanMsgs
*
* @return javax.swing.JTextPane
*/
public JTextPane getTxtpanMsgs() {
if (txtpanMsgs == null) {
txtpanMsgs = new JTextPane();
}
return txtpanMsgs;
}

/**
* This method initializes jPanel5
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel5() {
if (jPanel5 == null) {
jPanel5 = new JPanel();
jPanel5.setLayout(new GridBagLayout());
jPanel5.setPreferredSize(new Dimension(0, 30));
}
return jPanel5;
}

/**
* This method initializes jPanel6
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel6() {
if (jPanel6 == null) {
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(1);
gridLayout.setHgap(10);
gridLayout.setVgap(10);
gridLayout.setColumns(4);
jPanel6 = new JPanel();
jPanel6.setLayout(gridLayout);
jPanel6.setPreferredSize(new Dimension(0, 30));
jPanel6.add(getJPanel7(), null);
jPanel6.add(getJPanel8(), null);
jPanel6.add(getButClose(), null);
jPanel6.add(getButSend(), null);
}
return jPanel6;
}

/**
* This method initializes txtMsg
*
* @return javax.swing.JTextArea
*/
private JTextArea getTxtMsg() {
if (txtMsg == null) {
txtMsg = new JTextArea();
}
return txtMsg;
}

/**
* This method initializes butSend
*
* @return javax.swing.JButton
*/
private JButton getButSend() {
if (butSend == null) {
butSend = new JButton();
butSend.setText("发送");
butSend.addActionListener(this);
}
return butSend;
}

/**
* This method initializes butClose
*
* @return javax.swing.JButton
*/
private JButton getButClose() {
if (butClose == null) {
butClose = new JButton();
butClose.setText("关闭");
butClose.addActionListener(this);
}
return butClose;
}

/**
* This method initializes jPanel7
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel7() {
if (jPanel7 == null) {
jPanel7 = new JPanel();
jPanel7.setLayout(new GridBagLayout());
}
return jPanel7;
}

/**
* This method initializes jPanel8
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel8() {
if (jPanel8 == null) {
jPanel8 = new JPanel();
jPanel8.setLayout(new GridBagLayout());
}
return jPanel8;
}

@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==this.butClose){
//结束应用程序
System.exit(0);
}
if(e.getSource() ==this.butSend){
String txt=this.txtMsg.getText();
txt="<"+myName+">说:"+ txt;
out.println(txt);
out.flush();
//更新自己的聊天记录
String str=this.txtpanMsgs .getText();
str+=txt+"\n";
this.txtpanMsgs.setText(str);

//清空一下文本框
this.txtMsg.setText("");
}

}

}  //  @jve:decl-index=0:visual-constraint="27,-2"


运行一下ServerLoginFrame:



确定后:



登录客户端:



确定后:



聊天情况:





这个聊天的程序到这里并不算结束,群聊的功能也是可以实现的,有兴趣的可以先自己试试吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息