简单网络聊天程序设计(JAVA+Eclipse+JFrame)

一、已实现实现功能

1.用户的注册、登录、退出功能(只能由服务器端来操作数据库来添加和查找用户,用户没有直接操作数据库的权限)

2.由服务器转发用户发来的用户状态信息、私聊信息,公聊信息,请求发送文件信息、文件发送响应信息

3.服务器转发文件发送信息和文件响应信息,然后两个客户端直接建立socket连接来发送文件,用户可以选择是否接收文件

4.服务器端可以从数据库删除已注册的用户,并给其他用户发送用户下线信息,已删除的用户不能再发送信息

源代码:https://download.csdn.net/download/wmrem/10827465

二、程序用到的主要组件,及其功能实现

    1. JButton-实现用户登录、注册、发送文件和发送聊天信息的功能

      双击Button按钮,添加Button的单击触发事件方法,在函数里添加功能实现代码。调用button的setText("button的名称")    设置button的名称,setEnabled(true)可将按钮设为可用状态。

// 将发送文件按钮设为可用状态;将发送消息按钮设为
btnSendMsg.setEnabled(true);
btnSendFile.setEnabled(true);
checkBoxPrivateChat.setEnabled(true);
// 将“登录”按钮设为“退出”按钮
btnLogon.setText("退出");

    2. JTextFile-用于输入用户名,聊天信息

    JTextFile主要是用setText()方法来获取文本框的值

    localUserName = textFieldUserName.getText().trim();// 获取用户名并删除多余空格

    3. JPasswordField-用于输入用户密码

    localPassword = new String(passwordFieldPwd.getPassword());

    4. JTextPane-记录用户的各种操作信息

	// 向消息记录文本框中添加一条消息记录
	private void addMsgRecord(final String msgRecord, Color msgColor, int fontSize, boolean isItalic,
			boolean isUnderline) {
		final SimpleAttributeSet attrset = new SimpleAttributeSet();// 简单属性集
		StyleConstants.setForeground(attrset, msgColor);// 字体颜色
		StyleConstants.setFontSize(attrset, fontSize);
		StyleConstants.setUnderline(attrset, isUnderline);// 下划线
		StyleConstants.setItalic(attrset, isItalic);
		// 界面线程的事件队列,创建一个任务封装为runnable对象,加入队列,依次取,做任务,防止网络信息的丢失;
		// 更新界面的操作都要用EventQueue.invokeLater不然会造成:后台线程丢失数据,界面花掉,失去响应
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				// 把文本添加到消息记录文本框中
				Document docs = textPaneMsgRecord.getDocument();
				try {
					docs.insertString(docs.getLength(), msgRecord, attrset);
				} catch (BadLocationException e) {
					e.printStackTrace();
				}
			}
		});
	}
    insertComponen(组件)方法可以在JTextPane上添加组件
// 进度条加入消息记录文本框
JProgressBar fileSendProcessBar = new JProgressBar();
EventQueue.invokeLater(new Runnable() {
	public void run() {
		textPaneMsgRecord.insertComponent(fileSendProcessBar);
	}
});

    5. JList-显示当前在线用户列表

 

        使用DefaultListModel创建在线用户列表,并添加addListSelectionListener(new ListSelectionListener()事件,当某一个在线用户被选中时,获取用户名,用于私聊与发送文件功能的实现

        // 在线用户列表
	private DefaultListModel<String> onlineUserDlm = new DefaultListModel<String>();
        listOnlineUsers = new JList<String>(onlineUserDlm);
	//点击获取在线用户名
	listOnlineUsers.addListSelectionListener(new ListSelectionListener() {
		public void valueChanged(ListSelectionEvent e) {
			if (!listOnlineUsers.getValueIsAdjusting()) {// 选中最后点击的人
				privateChatDstUser = listOnlineUsers.getSelectedValue();
				System.err.println("选中的收信人为" + privateChatDstUser);// 每点击一次输出一次
			}
		}
	});

    6. JCheckBox-实现私聊

        JCheckBox是多选按钮,该选项被选中时可以实现发送私聊信息的功能。

// 创建聊天消息,收信人为空时为公聊消息
ChatMessage chatMessage = new ChatMessage(localUserName, "", msgContent);
if (checkBoxPrivateChat.isSelected() && privateChatDstUser != null) {// 发送私聊了消息
	chatMessage = new ChatMessage(localUserName, privateChatDstUser, msgContent);
	System.out.println("私聊收信人:" + privateChatDstUser);
} else if (checkBoxPrivateChat.isSelected() && privateChatDstUser == null) {
	JOptionPane.showMessageDialog(Client.this, "请选择选择私聊聊天对象");
	return;
}

    7.JProgressBar-用进度条条显示文件发送进程

// 进度条并加入消息记录文本框
JProgressBar fileSendProcessBar = new JProgressBar();
EventQueue.invokeLater(new Runnable() {//防止界面花掉
	public void run() {
		textPaneMsgRecord.insertComponent(fileSendProcessBar);
	}
});
addMsgRecord("\r\n", Color.blue, 12, false, false);
fileSendProcessBar.setVisible(true);// 进度条可见
fileSendProcessBar.setStringPainted(true);// 设置进度条上的字符串显示,false则不能显示
fileSendProcessBar.setForeground(SystemColor.activeCaption);
fileSendProcessBar.setString(prcent);//设置进度条上显示的字符串
fileSendProcessBar.setMaximum(100);//进度条的终点
fileSendProcessBar.setMinimum(0);//进度条的起点
fileSendProcessBar.setValue(value);//设置进度条的完成进度
fileSendProcessBar.setString(prcent + " 当前文件接收速度࿱
  • 13
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 24
    评论
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值