实验七 图形界面聊天程序.docx_第1页
实验七 图形界面聊天程序.docx_第2页
实验七 图形界面聊天程序.docx_第3页
实验七 图形界面聊天程序.docx_第4页
实验七 图形界面聊天程序.docx_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

课程 高级JAVA开发技术 _ 实验名称 实验七 图形界面聊天程序 一、 实验目的Swing组件使用二、 实验内容1、 登陆界面添加事件处理4.G.12、 注册界面添加事件处理4.G.23、 客户端聊天主窗口发送和接收信息功能5.G.14、 客户端聊天主窗口发送和接收用户信息功能5.G.25、 聊天服务器端功能5.G.3三、 实验过程及结果程序源码及结果:package com.haiersoft.chat.client;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class FrmLogin extends JFrame implements ActionListenerprivate JPanel p;private JLabel lblName, lblPwd;private JTextField txtName;private JPasswordField txtPwd;private JButton btnOk, btnCancle, btnRegist;public FrmLogin() super(聊天室登录);p = new JPanel();p.setLayout(null);lblName = new JLabel(用户名);lblPwd = new JLabel(密码);txtName = new JTextField(20);txtPwd = new JPasswordField(20);txtPwd.setEchoChar(*);btnOk = new JButton(确定);btnCancle = new JButton(取消);btnRegist = new JButton(注册);lblName.setBounds(30, 30, 60, 25);txtName.setBounds(95, 30, 120, 25);lblPwd.setBounds(30, 60, 60, 25);txtPwd.setBounds(95, 60, 120, 25);btnOk.setBounds(30, 90, 60, 25);btnCancle.setBounds(95, 90, 60, 25);btnRegist.setBounds(160, 90, 60, 25);btnOk.addActionListener(this);btnCancle.addActionListener(this);btnRegist.addActionListener(this);p.add(lblName);p.add(txtName);p.add(lblPwd);p.add(txtPwd);p.add(btnOk);p.add(btnCancle);p.add(btnRegist);this.add(p);this.setSize(250, 170);this.setLocation(300, 300);this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public void actionPerformed(ActionEvent e) Object source = e.getSource();if (source = btnOk) btnOkClick();if (source = btnCancle) btnCancleClick();if (source = btnRegist) btnRegistClick();private void btnOkClick() String username = txtName.getText();String userpwd = new String(txtPwd.getPassword();if (username.equals() JOptionPane.showMessageDialog(btnOk, 用户名不能为空, 提示, JOptionPane.WARNING_MESSAGE);return;if (userpwd.equals() JOptionPane.showMessageDialog(btnOk, 密码不能为空, 提示, JOptionPane.WARNING_MESSAGE);return;if (userpwd.equals(haiersoft) this.setVisible(false);FrmMain frmMain = new FrmMain();frmMain.setVisible(true); else JOptionPane.showMessageDialog(btnOk, 错误的用户名或密码, 提示, JOptionPane.WARNING_MESSAGE);private void btnCancleClick() txtName.setText();txtPwd.setText();private void btnRegistClick() this.setVisible(false);FrmRegist frmRegist = new FrmRegist();frmRegist.setVisible(true);public static void main(String args) FrmLogin frmLogin = new FrmLogin();frmLogin.setVisible(true);package com.haiersoft.chat.client;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;public class FrmRegist extends JFrame implements ActionListener private JPanel p;private JLabel lblName, lblNickname, lblPwd, lblRePwd, lblSex, lblImage;private JTextField txtName, txtNickname;private JPasswordField txtPwd, txtRePwd;private JRadioButton rbMale, rbFemale;private JRadioButton ckbImage = new JRadioButton8;private JButton btnOk, btnCancle, btnReturn;private String checkedHeadImg = null;public FrmRegist() super(用户注册);p = new JPanel(null);lblName = new JLabel(用户名:);lblNickname = new JLabel(昵称:);lblPwd = new JLabel(密码:);lblRePwd = new JLabel(确认密码:);lblSex = new JLabel(性别:);lblImage = new JLabel(头像:);txtName = new JTextField(20);txtNickname = new JTextField(20);txtPwd = new JPasswordField(20);txtRePwd = new JPasswordField(20);rbMale = new JRadioButton(男);rbMale.setSelected(true);rbFemale = new JRadioButton(女);JPanel pimg = new JPanel(new GridLayout(2, 5);ButtonGroup bgimg = new ButtonGroup();for (int i = 0; i ckbImage.length; i+) ckbImagei = new JRadioButton(String.valueOf(i + 1),new ImageIcon(images/ + (i + 1) + .gif);bgimg.add(ckbImagei);pimg.add(ckbImagei);btnOk = new JButton(确定);btnCancle = new JButton(取消);btnReturn = new JButton(返回);btnOk.addActionListener(this);btnCancle.addActionListener(this);btnReturn.addActionListener(this);ButtonGroup bgsex = new ButtonGroup();bgsex.add(rbMale);bgsex.add(rbFemale);lblName.setBounds(30, 30, 60, 25);txtName.setBounds(95, 30, 200, 25);lblNickname.setBounds(30, 60, 60, 25);txtNickname.setBounds(95, 60, 200, 25);lblPwd.setBounds(30, 90, 60, 25);txtPwd.setBounds(95, 90, 200, 25);lblRePwd.setBounds(30, 120, 60, 25);txtRePwd.setBounds(95, 120, 200, 25);lblSex.setBounds(30, 150, 60, 25);rbMale.setBounds(95, 150, 60, 25);rbFemale.setBounds(160, 150, 60, 25);lblImage.setBounds(30, 180, 60, 25);pimg.setBounds(95, 180, 280, 100);btnOk.setBounds(90, 290, 60, 25);btnCancle.setBounds(155, 290, 60, 25);btnReturn.setBounds(220, 290, 60, 25);p.add(lblName);p.add(txtName);p.add(lblNickname);p.add(txtNickname);p.add(lblPwd);p.add(txtPwd);p.add(lblRePwd);p.add(txtRePwd);p.add(lblSex);p.add(rbMale);p.add(rbFemale);p.add(lblImage);p.add(pimg);p.add(btnOk);p.add(btnCancle);p.add(btnReturn);this.add(p);this.setSize(400, 380);this.setLocation(300, 300);this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public void actionPerformed(ActionEvent e) Object source = e.getSource();if (source = btnOk) btnOkClick();if (source = btnCancle) btnCancleClick();if (source = btnReturn) btnReturnClick();private void btnOkClick() String username = txtName.getText();String nickname = txtNickname.getText();String userpwd = new String(txtPwd.getPassword();String repwd = new String(txtRePwd.getPassword();int sex = 0;if (rbFemale.isSelected() sex = 1;if (username.equals() JOptionPane.showMessageDialog(btnOk, 用户名不能为空, 提示, JOptionPane.WARNING_MESSAGE);return;if (nickname.equals() JOptionPane.showMessageDialog(btnOk, 昵称不能为空, 提示, JOptionPane.WARNING_MESSAGE);return;if (userpwd.equals() JOptionPane.showMessageDialog(btnOk, 密码不能为空, 提示, JOptionPane.WARNING_MESSAGE);return;if (userpwd.length() 10) JOptionPane.showMessageDialog(btnOk, 密码长度范围在610之间, 提示, JOptionPane.WARNING_MESSAGE);return;if (repwd.equals() JOptionPane.showMessageDialog(btnOk, 确认密码不能为空, 提示, JOptionPane.WARNING_MESSAGE);return;if (!repwd.equals(userpwd) JOptionPane.showMessageDialog(btnOk, 确认密码和密码必须相同, 提示, JOptionPane.WARNING_MESSAGE);return;if (userpwd.equals(haiersoft) JOptionPane.showMessageDialog(btnOk, 注册成功!, 提示, JOptionPane.INFORMATION_MESSAGE);btnCancleClick(); else JOptionPane.showMessageDialog(btnOk, 注册失败!, 提示, JOptionPane.INFORMATION_MESSAGE);private void btnCancleClick() txtName.setText();txtNickname.setText();txtPwd.setText();txtRePwd.setText();rbMale.setSelected(true);private void btnReturnClick() this.setVisible(false);FrmLogin frmLogin = new FrmLogin();frmLogin.setVisible(true);class ImageListener implements ItemListener public void itemStateChanged(ItemEvent e) JRadioButton source = (JRadioButton) e.getSource();if (source.isSelected() source.setBorderPainted(true);checkedHeadImg = source.getText() + .gif;System.out.println(checkedHeadImg); else source.setBorderPainted(false);public static void main(String args) FrmRegist frmRegist = new FrmRegist();frmRegist.setVisible(true);package com.haiersoft.util;import java.io.FileInputStream;import java.util.Properties;public class Config private static Properties p = null;static try p = new Properties();p.load(new FileInputStream(config/Config.ini); catch (Exception e) e.printStackTrace();public static String getValue(String key) return p.get(key).toString();package com.haiersoft.chat.client;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.ObjectOutputStream;import java.io.PrintWriter;import .Socket;import java.util.StringTokenizer;import javax.swing.*;import com.haiersoft.util.Config;public class FrmMain extends JFrame implements ActionListener private JSplitPane splitPaneV, splitPaneH;private JScrollPane spCenter;private JPanel pRight;private JPanel pdown;private JTextArea txtContent;private JLabel lblSend;private JTextField txtSend;private JButton btnSend;private Socket socketMsg;private PrintWriter printMsg;private BufferedReader readMsg;private Socket socketUser;private ObjectOutputStream printUser;private BufferedReader readUser;public static String username = haier;public static String nickname = 海尔;public static String headimg = 8.gif;public FrmMain() super(聊天室);txtContent = new JTextArea();txtContent.setEditable(false);spCenter = new JScrollPane(txtContent);pdown = new JPanel();lblSend = new JLabel(输入:);txtSend = new JTextField(20);btnSend = new JButton(发送);btnSend.addActionListener(this);pdown.add(lblSend);pdown.add(txtSend);pdown.add(btnSend);pRight = new JPanel(null);splitPaneV = new JSplitPane(JSplitPane.VERTICAL_SPLIT, spCenter, pdown);splitPaneV.setDividerLocation(320);splitPaneH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, splitPaneV, pRight);splitPaneH.setDividerLocation(350);this.add(splitPaneH);this.setSize(500, 400);this.setLocation(300, 300);this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);createMsgConnection();new GetMsgFromServer().start();createUserListConnection();new GetUserListFromServer().start();public void actionPerformed(ActionEvent e) Object source = e.getSource();if (source = btnSend) String str = txtSend.getText();if (!str.equals() printMsg.println(nickname + : + str);printMsg.flush();txtSend.setText();private void createMsgConnection() try socketMsg = new Socket(Config.getValue(server), Integer.parseInt(Config.getValue(msgport);printMsg = new PrintWriter(socketMsg.getOutputStream(), true); catch (Exception e) e.printStackTrace();private void createUserListConnection() try socketUser = new Socket(Config.getValue(server), Integer.parseInt(Config.getValue(userport);printUser = new ObjectOutputStream(socketUser.getOutputStream();User user = new User(username, nickname, headimg);printUser.writeObject(User) user);printUser.flush(); catch (Exception e) e.printStackTrace();class GetMsgFromServer extends Thread private String strMsg = null;public GetMsgFromServer() super();public void run() try readMsg = new BufferedReader (new InputStreamReader(socketMsg.getInputStream();dostrMsg = readMsg.readLine();txtContent.append(strMsg + n); while (strMsg != null); catch (Exception e) e.printStackTrace();class GetUserListFromServer extends Thread public GetUserListFromServer() super();public void run() try readUser = new BufferedReader (new InputStreamReader(socketUser.getInputStream();do String line = readUser.readLine();if (line != null) StringTokenizer st = new StringTokenizer(line, &);pRight.removeAll();int y = 10;while (st.hasMoreTokens() String name = st.nextToken();String img = st.nextToken();System.out.println(name + : + img);JLabel userNode = new JLabel (name, new ImageIcon(image/ + img), JLabel.LEFT);userNode.setBounds(10, y, 140, 40);pRight.add(userNode);y+=45;pRight.setSize(150, 300);splitPaneH.setDividerLocation(350);FrmMain.this.setSize(0, 0);FrmMain.this.setSize(500, 400); while (true); catch (Exception e) e.printStackTrace();public static void main(String args) FrmMain frmMain = new FrmMain();frmMain.setVisible(true);package com.haiersoft.chat.client;import java.io.Serializable;public class User implements Serializable public String userName = ;public String nickName = ;public String headImg = ;public User(String username, String nickname, String headimg) this.userName = username;this.nickName = nickname;this.headImg = headimg;package com.haiersoft.chat.server;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.ObjectInputStream;import java.io.PrintWriter;import .ServerSocket;import .Socket;import java.util.ArrayList;import java.util.LinkedList;import javax.swing.*;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.DefaultTreeModel;import com.haiersoft.chat.client.User;import com.haiersoft.util.Config;public class ChatServer extends JFrame implements ActionListener private JSplitPane splitPaneV, splitPaneH;private JScrollPane spCenter, spRight;private JPanel pdown;private JTextArea txtContent;private JLabel lblSend;private JTextField txtSend;private JButton btnSend;private DefaultMutableTreeNode root;private DefaultTreeModel model;private JTree tree;private ServerSocket msgServerSocket;private ServerSocket userServerSocket;private ArrayList msgSocket = new ArrayList();private ArrayList printWriter = new ArrayList();private ArrayList bufferedReader = new ArrayList();private LinkedList msgList = new LinkedList();private ArrayList userSocket = new ArrayList();private ArrayList printUser = new ArrayList();private ArrayList readUser = new ArrayList();private ArrayList userList = new ArrayList();private static boolean isRun = true;public ChatServer() throws Exception super(服务器);txtContent = new JTextArea();txtContent.setEditable(false);spCenter = new JScrollPane(txtContent);pdown = new JPanel();lblSend = new JLabel(系统消息);txtSend = new JTextField(20);btnSend = new JButton(发送);btnSend.addActionListener(this);pdown.add(lblSend);pdown.add(txtSend);pdown.add(btnSend);splitPaneV = new JSplitPane(JSplitPane.VERTICAL_SPLIT, spCenter, pdown);splitPaneV.setDividerLocation(420);splitPaneH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, splitPaneV, spRight);splitPaneH.setDividerLocation(400);this.add(splitPaneH);this.setSize(600, 500);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);root = new DefaultMutableTreeNode(目前在线用户(用户名:昵称));model = new DefaultTreeModel(root);tree = new JTree(model);spRight = new JScrollPane(tree);msgServerSocket = new ServerSocket (Integer.parseInt(Config.getValue(msgport);userServerSocket = new ServerSocket (Integer.parseInt(Config.getValue(userport);new AcceptUserSocketThread().start();new AcceptMsgSocketThread().start();new SendMessageThread().start();public void actionPerformed(ActionEvent e) if (e.getSource() = btnSend) String str = txtSend.getText();if (!str.equals() msgList.addFirst(系统消息: + str);private void sendUserListToClient() String people = ;for (int i = 0; i userList.size(); i+) people += & + userList.get(i).headImg;for (int i = 0; i printUser.size(); i+) if (printUser.get(i) != null) try printUser.get(i).println(people);printUser.get(i).flush(); catch (Exception e) e.printStackTrace();class AcceptUserSocketThread extends Thread public void run() while (isRun) try Socket socket = userServerSocket.accept();ObjectInputStream readerFromClient = new ObjectInputStream(socket.getInputStream();PrintWriter printToClient = new PrintWriter(socket.getOutputStream();printUser.add(printToClient);User user = (User) readerFromClient.readObject();if (user != null) userList.add(user);root.add(new DefaultMutableTreeNode(user.userName + : + user.nickName);tree.setModel(model);tree.updateUI();sendUserListToClient(); catch (Exception e) e.printStackTrace();class AcceptMsgSocketT

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论